r - ifelse 带有可能的字符串匹配列表,没有其他
问题描述
所以通常情况下,如果我想用 1s 填充新列 data$new.col,如果它在 data$strings 中找到字符串“foo”或“bar”,如果没有,我会使用这样的东西:
data$new.col <- ifelse(grepl("foo|bar",
data$strings, ignore.case = T, perl = T), "1", "0")
但是,我想在没有“其他”的情况下做同样的事情。我尝试使用简单的分配,但我一定做错了,因为它不起作用:
data$new.col[data$strings == "foo|bar"] <- "1"
提前致谢。
解决方案
您需要编辑过滤条件,如下所示:
data$new.col[data$strings == "foo" | data$strings == "bar"] <- "1"
推荐阅读
- php - 升级到 PHP 7.3 后的 CodeIgniter 问题
- c# - 访问最近文件中显示的文件目录
- mysql - SQL 存储函数有问题,返回错误
- go - 带有 CRUD 操作的 RW 锁
- r - 保留滞后价值以复合最终价值
- neo4j - 在 Neo4jClient 的 Where 子句中使用距离方法
- google-apps-script - 有没有办法使用特定脚本生成所有谷歌表格的列表?
- python - ImageDataGenerator:如何将第 4 维添加到 numpy 数组?
- android - 形状未应用于Android中的按钮选择器
- python - 从 SQLITE 表中检索值并用于查询另一个表