0

I have problem with R like in a title. I have a data frame with 40+ rows and some kind of data in 2 columns I am interested in. What I want to do is to check if in column 2 is value or NA, and if there is NA copy value from the same row in column 1 (might be empty as well). I don't know exactly what approach should be used in this situation.

Regards, RafaƂ

RafMil
  • 138
  • 2
  • 2
  • 15

1 Answers1

1

You can try using ifelse here:

df$col2 <- ifelse(is.na(df$col2), df$col1, df$col2)

This says, in English terms, to replace col2 with the value from col1 should the former be NA, otherwise leave the value as it was.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360