查找字符串是否在多个列中同时匹配并返回逻辑矩阵?

use*_*088 3 string grep design-patterns r matching

给定如下数据结构:

set.seed(10)
fruits <- c("apple", "orange", "pineapple")
fruits2 <- data.frame(id = 1:10, fruit1 = sample(fruits, 10, replace = T), fruit2 =   sample(fruits, 10, replace = T), fruit3 = sample(fruits, 10, replace = T))

> fruits2
   id    fruit1    fruit2    fruit3
1   1    orange    orange pineapple
2   2     apple    orange    orange
3   3    orange     apple pineapple
4   4 pineapple    orange    orange
5   5     apple    orange    orange
6   6     apple    orange pineapple
7   7     apple     apple pineapple
8   8     apple     apple     apple
9   9    orange    orange pineapple
10 10    orange pineapple    orange
Run Code Online (Sandbox Code Playgroud)

我可以轻松地测试data.frame中的任何位置是否与给定的字符串完全相等,fruits2 == "mystring"它将返回一个非常方便的格式.例如:

fruits2 == "orange"
         id fruit1 fruit2 fruit3
 [1,] FALSE   TRUE   TRUE  FALSE
 [2,] FALSE  FALSE   TRUE   TRUE
 [3,] FALSE   TRUE  FALSE  FALSE
 [4,] FALSE  FALSE   TRUE   TRUE
 [5,] FALSE  FALSE   TRUE   TRUE
 [6,] FALSE  FALSE   TRUE  FALSE
 [7,] FALSE  FALSE  FALSE  FALSE
 [8,] FALSE  FALSE  FALSE  FALSE
 [9,] FALSE   TRUE   TRUE  FALSE
[10,] FALSE   TRUE  FALSE   TRUE
Run Code Online (Sandbox Code Playgroud)

但是,我真正想做的是搜索模式(例如"apple")并返回相同的格式.也就是说,我希望能够测试data.frame中的每个项目中的包含(但不一定等于)字符串"apple"并返回相同的逻辑矩阵.在这种情况下,我希望它产生:

         id fruit1 fruit2 fruit3
 [1,] FALSE  FALSE  FALSE   TRUE
 [2,] FALSE   TRUE  FALSE  FALSE
 [3,] FALSE  FALSE   TRUE   TRUE
 [4,] FALSE   TRUE  FALSE  FALSE
 [5,] FALSE   TRUE  FALSE  FALSE
 [6,] FALSE   TRUE  FALSE   TRUE
 [7,] FALSE   TRUE   TRUE   TRUE
 [8,] FALSE   TRUE   TRUE   TRUE
 [9,] FALSE  FALSE  FALSE   TRUE
[10,] FALSE  FALSE   TRUE  FALSE
Run Code Online (Sandbox Code Playgroud)

有没有简单的方法在R中执行此操作而不指定多个模式(在这种情况下我知道 fruits2 == "apple" | fruits2 == "pineapple"会这样做,但在我的真实数据集中枚举所有可能的字符串以完全匹配是不可能的)?

我认为有解决方法,我可以编写一个函数来使用它,grepl()但我想知道是否有一个更简单的解决方案.

har*_*mug 5

在基地R,

> apply(fruits2,2,function(x){grepl("apple",x)})
         id fruit1 fruit2 fruit3
 [1,] FALSE  FALSE  FALSE   TRUE
 [2,] FALSE   TRUE  FALSE  FALSE
 [3,] FALSE  FALSE   TRUE   TRUE
 [4,] FALSE   TRUE  FALSE  FALSE
 [5,] FALSE   TRUE  FALSE  FALSE
 [6,] FALSE   TRUE  FALSE   TRUE
 [7,] FALSE   TRUE   TRUE   TRUE
 [8,] FALSE   TRUE   TRUE   TRUE
 [9,] FALSE  FALSE  FALSE   TRUE
[10,] FALSE  FALSE   TRUE  FALSE

 n = 10000
 fruits2 <- data.frame(id = 1:n, fruit1 = sample(fruits, n, replace = T), fruit2 =   sample(fruits, n, replace = T), fruit3 = sample(fruits, n, replace = T))

> system.time(apply(fruits2,2,function(x){grepl("apple",x)}))   
  user  system elapsed 
  0.016   0.000   0.019 

> system.time(colwise(myfun)(fruits2))
  user  system elapsed 
  0.016   0.000   0.017

> system.time(sapply(fruits2,function(x) grepl('apple',x)))
   user  system elapsed 
  0.032   0.000   0.034
Run Code Online (Sandbox Code Playgroud)

正如@eddi指出的那样,lapply确实是最快的:

> system.time(do.call("cbind",lapply(colnames(fruits2),function(x) grepl('apple',fruits2[,x]))))
   user  system elapsed 
  0.016   0.000   0.016
Run Code Online (Sandbox Code Playgroud)

  • 我认为`colwise`解决方案由于缓存而显得更快.如果你查看它的代码,它只是在经过一些检查后调用`lapply`. (2认同)