AEM
AEM

Reputation: 948

Merging columns and removing NA

I have a data frame:

A<- c(NA, 1, 2, NA, 3, NA)
R<- c(2, 1, 2, 1, NA, 1)
C<- c(rep ("B",3), rep ("D", 3))
data1<-data.frame (A,R,C)
data1

And I wan to merge column A and R, to have a data frame like data2

AR<- c(2, 1, 2, 1, 3, 1)
C<- c(rep ("B",3), rep ("D", 3))
data2<-data.frame (AR,C)
data2

Do you know how I can do that?

Upvotes: 0

Views: 1941

Answers (1)

rrs
rrs

Reputation: 9903

You might want to consider what happens if "A" and "R" have different values, but this should work:

data2 <- with(data1, data.frame(AR=ifelse(is.na(A), R, A), C=C))

Upvotes: 1

Related Questions