r - 跨列表应用 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
提前致谢!
解决方案
您需要%in%
操作员:
df$b <- ifelse(df$value %in% df1,'match',NA)
推荐阅读
- html - 如何从 url 中删除端口以使 xampp 数据库中的图像源正常工作?
- javascript - javacript 字符串文字意外行为
- python - Django DecimalField 不一致的舍入
- python - 检查迭代期间字典值中是否存在子字符串
- c# - 发布 Nuget 包时有关 PackageReference 版本的最佳实践?
- sql - 在预期条件的上下文中指定的非布尔类型表达式 Near LEFT JOIN USER
- asp.net - 在阿拉伯语 Web 开发中使用 itextsharp 导出 pdf 文件中的 gridview
- sql - MariaDB 设置数据传输大小
- ios - Swift 测试中的 @testable 导入问题
- rjags - 如何在 rjags 中为非负因子载荷指定尖峰和平板先验