首页 > 解决方案 > 跨列表应用 ifelse 语句

问题描述

尝试将列与列表匹配,如果存在匹配则返回“匹配”ifelse

我接近了这个尝试:

> df$b <- ifelse(grepl(df1,df$value), 'match', NA)


Warning message:
In grepl(df1, df$value) :
  argument 'pattern' has length > 1 and only the first element will be used

 value b    
   <int> <chr>
 1     1 NA   
 2     2 NA   
 3     3 NA   
 4     4 NA   
 5     5 match
 6     6 NA   
 7     7 NA   
 8     8 NA   
 9     9 NA   
10    10 NA

我想要得到的是:

value b 
   <int> <chr>
 1     1 NA   
 2     2 NA   
 3     3 NA   
 4     4 NA   
 5     5 match
 6     6 match   
 7     7 match   
 8     8 match   
 9     9 match   
10    10 match  

提前致谢!

标签: rif-statementlogic

解决方案


您需要%in%操作员:

df$b <- ifelse(df$value %in% df1,'match',NA)

推荐阅读