# Fill a missing value of a variable with the value of the previous column (same row)

I'd like to fill the missing values of the variable `xxx.2` following dataset x:

``````  sample.code day.code    var xxx.1 xxx.2
1           a      a_1    rff    10   11
2           a      a_1   drav    10   NA
3           a      a_1 proint    15   15
4           a      a_1   jock    NA   NA
5           a      a_1    saf     8    8
6           a      a_1   prut     9   NA
``````
``````x <- data.frame(
sample.code = as.factor(c("a", "a", "a", "a", "a", "a")),
day.code = as.factor(c("a_1", "a_1", "a_1", "a_1", "a_1", "a_1")),
var = as.factor(c("rff", "drav", "proint", "jock", "saf", "prut")),
xxx.1 = c(10L, 10L, 15L, NA, 8L, 9L),
xxx.2 = c(11L, NA, 15L, NA, 8L, NA)
)
``````

in order to obtain the following matrix:

``````  sample.code day.code    var xxx.1 xxx.2
1           a      a_1    rff    10   11
2           a      a_1   drav    10   10
3           a      a_1 proint    15   15
4           a      a_1   jock    NA   NA
5           a      a_1    saf     8    8
6           a      a_1   prut     9    9
``````

so that the values of the column `xxx.2` have to be filled by the value of the same row, reported in the variable `xxx.1`.
If both `xxx.1` and `xxx.2` are missing, I'd like to have `NA` for those samples.

I tried to use the following code:

``````while(length(ind <- which(data\$Allele.2 == "")) > 0){
data\$Allele.2[ind] <- data\$Allele.1[ind]
}
``````

but I didn't succed.
I'd be very grateful if anybody could be attend to this matter.

This is one way:

``````x\$xxx.2 <- ifelse(is.na(x\$xxx.1), x\$xxx.2, x\$xxx.1)
``````
2 Likes

Thank you very much for your help, you actually solved my issue!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.