【问题标题】:Ifelse statement applied across list [duplicate]跨列表应用的 ifelse 语句[重复]
【发布时间】:2019-07-05 18:25:12
【问题描述】:

使用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  

提前致谢!

【问题讨论】:

  • 在标记为重复后编辑了问题。看到其他答案,我永远无法回答我的问题。我也找不到那个问题。
  • 你说得对,我应该在标记为重复之前进行编辑。无论如何,警告来自df$value == df1,这是在您编辑之前写的。骗子回答了它。

标签: r if-statement logic


【解决方案1】:

您需要%in% 运算符:

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

【讨论】:

  • 谢谢,这完全有效。
猜你喜欢
  • 1970-01-01
  • 2020-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-26
  • 2019-08-03
  • 2015-10-03
  • 1970-01-01
相关资源
最近更新 更多