Ifelse not working

I am using ifelse to filter from a vector of characters.




ifelse(RCM %in% "Tom",file_list[!file_list%in%rmd_list],0)

The output suppose to show "a.csv" and "b.csv"

The ifelse() function returns a vector the same length as the test expression, which in this case is 1.

length(RCM %in% "Tom")
#> [1] 1 

Instead, you can use if...else.

if(RCM %in% "Tom") {file_list[!file_list %in% rmd_list]} else {0}
#> [1] "a.csv" "b.csv"

Created on 2023-02-03 with reprex v2.0.2.9000

1 Like

ifelse steps along the vectors in its arguments. Since RCM is of length 1, it only returns one value. This illustration might help you see what ifelse does.

ifelse(c("Tom","Brad","Alice","Tom") %in% "Tom", c(1,2,3,4),c(11,22,33,44))
[1]  1 22 33  4

Compare with

 "Tom" %in% RCM

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.