In the following data frame,
col1 <- c("g1","g2","g3",NA,"g4",NA)
col2 <- c(NA,"a1","a2",NA,"a3","a4")
df1 <-data.frame(col1,col2)
I would like to replace the rows with NA in col1 with corresponding rows of col2. Is it correct to proceed by extracting the rows containing NA by
row <- which(is.na(col1))
and then extract the characters from col2 by
extract <- df1$col2[row]
After this I have no clue how to replace the NAs in col1 with the extracted characters. Please help!
df1$col1[is.na(df1$col1)] <- df1$col2[is.na(df1$col1)]