【问题标题】:Find multiple strings in entire dataframe [duplicate]在整个数据框中查找多个字符串[重复]
【发布时间】:2019-10-28 04:55:29
【问题描述】:

我正在尝试使用 which 函数在我的数据框中查找多个字符串。我正在尝试从Find string in data.frame扩展答案

一个示例数据框是:

df1 <- data.frame(animal=c('a','b','c','two', 'five', 'c'), level=c('five','one','three',30,'horse', 'five'), length=c(10, 20, 30, 'horse', 'eight', 'c'))

1      a  five     10
2      b   one     20
3      c three     30
4    two    30  horse
5   five horse  eight
6      c  five      c 

当我对一个字符串应用 which 函数时,在这个数据帧上,我得到了正确的输出,例如 which(df1 =="c" , arr.ind = T);df1 给出:

  row col
[1,]   3   1
[2,]   6   1
[3,]   6   3

但是当我尝试搜索多个字符串时,我只能得到部分正确的输出,例如 which(df1 ==c("c", "horse", "five") , arr.ind = T)

  row col
[1,]   5   2
[2,]   6   2

预期的输出应该是:

     row col
[1,]   3   1
[2,]   5   1
[3,]   6   1
[4,]   1   2
[5,]   5   2
[6,]   6   2
[7,]   4   3
[8,]   6   3

因此我的问题是:

  1. 为什么使用 c("c", "horse", "five") 的解决方案不起作用?

  2. 我已经尝试过

which(df1=="c" | df1=="horse" | df1 =="five", arr.ind = T)

这给了我正确的输出,但是对于许多字符串来说太长了, 如何让我的代码简洁?

【问题讨论】:

  • 你也可以这样做:purrr::map(df1,function(x) which(x%in%c("c", "horse", "five") , arr.ind = T))
  • @NelsonGon 正准备用它进行编辑。您可以使用sapplywhich(sapply(df1, function(x) x %in% vals), arr.ind = TRUE)

标签: r dataframe


【解决方案1】:

我们可以使用lapply 遍历向量,使用| 对单个逻辑矩阵执行==Reduce 并使用which 进行换行

which(Reduce(`|`, lapply(c("c", "horse", "five"), `==`, df1)), arr.ind = TRUE)
#     row col
#[1,]   3   1
#[2,]   5   1
#[3,]   6   1
#[4,]   1   2
#[5,]   5   2
#[6,]   6   2
#[7,]   4   3
#[8,]   6   3

或者另一种选择是使用mutate_all 循环遍历数据集的列并使用which 换行

library(dplyr)
df1 %>%
  mutate_all(list(~ . %in% c("c", "horse", "five"))) %>%
  as.matrix %>% 
  which(., arr.ind = TRUE)

注意:在这里,如果 OP 想要进行完整的字符串匹配,我们不需要任何正则表达式或部分匹配。它应该比进行任何部分匹配更快


通常,对于多个元素 %in% 会很有用,但它只适用于矢量而不适用于 data.frame

【讨论】:

    【解决方案2】:

    由于您有多个值,因此您无法在数据框中直接比较它们。一种方法是通过创建字边界将sapplygrepl 一起使用,并检查模式是否存在于任何列中,然后使用which 获取行和列索引。

    vals <- c("c", "horse", "five") 
    
    which(sapply(df1, grepl, pattern = paste0("\\b", vals, "\\b", collapse = "|")), 
          arr.ind = TRUE)
    
    #     row col
    #[1,]   3   1
    #[2,]   5   1
    #[3,]   6   1
    #[4,]   1   2
    #[5,]   5   2
    #[6,]   6   2
    #[7,]   4   3
    #[8,]   6   3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 2012-01-16
      • 2014-02-11
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 2018-09-05
      相关资源
      最近更新 更多