copy a values from another column

Hi Team,
I'm trying to conditionally copy a value from another column. The logical is: if column X1 content "keyword1" then copy column X1 value to keyword1 column, else X2 ... X5, if all of columns do not content "keyword1" value, then keyword1 column is NA.

I wrote the code below. It can ran, but keyword1 column doesn't copy the value. I'm sure X1 ---- X5 column should content 'keyword1' string. I'm not sure why it doesn't copy the corresponding column into keyword1 column.

Could you help me?

Thanks,
Kai

try2$keyword1 <-
         ifelse(try2$X1 %in% "keyword1", try2$X1,
                ifelse(try2$X2 %in% "keyword1", try2$X2,
                       ifelse(try2$X3 %in% "keyword1", try2$X3,
                              ifelse(try2$X4 %in% "keyword1", try2$X4,
                                     ifelse(try2$X5 %in% "keyword1", try2$X5,NA
 ) ) ) ) )

One thing you have to do is invert the logic of your tests. You want to test
keyword1 %in% try2$X1
not
try2$X1 %in% "keyword1"

Thanks FJCC. I fixed the code.
It seems: the last "ifelse" will overwrite the previous "ifelse" result. I mean, it works only try2$X5 contents "keyword1". if any of try2$X1, X2, X3 or X4 contents "keyword1", but X5 doesn't contents "keyword1", the result still evaluate X5. And put NA into keyword1 column.

Is it possible to do this: if any of X1,2,3,4,5 having "keyword1", then copy the column value to keyword1 column and do not look at the rest of column (avoid NA to replace the existing value)?

Thanks,
Kai

Instead of %in% , try testing with grepl().

try2 <- data.frame(X1 = c("a","Zkeyword1","b","C","D"),
                   X2 = c("keyword1W","A","B","C","D"),
                   X3 = c("A","B","C","D","Kkeyword1"),
                   X4 = c("A","B","C","keyword1D","D"),
                   X5 = c("A","B","keyword1S","C","D"))  

try2
#>          X1        X2        X3        X4        X5
#> 1         a keyword1W         A         A         A
#> 2 Zkeyword1         A         B         B         B
#> 3         b         B         C         C keyword1S
#> 4         C         C         D keyword1D         C
#> 5         D         D Kkeyword1         D         D
try2$keyword1 <-
  ifelse(grepl("keyword1",try2$X1), try2$X1,
         ifelse(grepl("keyword1",try2$X2), try2$X2,
                ifelse(grepl("keyword1",try2$X3), try2$X3,
                       ifelse(grepl("keyword1",try2$X4), try2$X4,
                              ifelse(grepl("keyword1",try2$X5), try2$X5,NA
                              ) ) ) ) )

try2
#>          X1        X2        X3        X4        X5  keyword1
#> 1         a keyword1W         A         A         A keyword1W
#> 2 Zkeyword1         A         B         B         B Zkeyword1
#> 3         b         B         C         C keyword1S keyword1S
#> 4         C         C         D keyword1D         C keyword1D
#> 5         D         D Kkeyword1         D         D Kkeyword1

Created on 2022-11-14 with reprex v2.0.2

Hi FJCC,
Thank you very much! It is working.
Best,
Kai

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.