Hello Team,
I have a working code below, it scan all of columns (the column name start with 'data') and put the correct value into a field
try2.un$keyword2 <-
ifelse(grepl("keyword2",try2.un$data1), try2.un$data1,
ifelse(grepl("keyword2",try2.un$data2), try2.un$data2,
ifelse(grepl("keyword2",try2.un$data3), try2.un$data3,
ifelse(grepl("keyword2",try2.un$data4), try2.un$data4,
ifelse(grepl("keyword2",try2.un$data5), try2.un$data5,NA
) ) ) ) )
Because the columns of data1 .. 5 were created in dynamic way, so it has 5 fields this time, it maybe 15 columns next time. So, I learn from internet to write the code below:
for(i in grep("data", names(df))) {
is.na(try2.un$keyword2) <- !grepl("keyword2", df[[i]])
}
It is no error message, but try2.un$keyword2 is NA. I guess, it use data5 field value to update keyword2 field. I know there is not 'keyword2' if data5, but it may have it in other situation. And I'm sure the column had values in old way. Could you look at it and tell me how to fix it?
Thanks,
Kai