I have two columns of data consisting number and NA entries. There is no row in which both columns have NA as that row's entry, but the columns both have NA as entries for some rows. I would like to make a new vector. If one of my two initial columns has a numerical value, I would like the new vector's entry for that row to be that numerical value. So going from this:
c1 c2
1 NA
2 NA
NA 3
to this:
c1 c2 c3
1 NA 1
2 NA 2
NA 3 3
I can do this with for loops:
for (i in 1:length(c1)) {
if (!is.na(c1[i])) {
c3[i] <- c1[i]
} else {
if (!is.na(c2[i])) {
c3[i] <- c2[i]
}
}
}
But I am assuming that there is a cleaner way to do it that takes advantage of R's features. Thank you!