如何在不同(未指定)列中过滤出包含两个特定字符串的行?

时间:2019-04-08 19:12:51

标签: r subset

给定一组8个名称,我想生成5个名称的所有唯一组合。但是,某些名称可能不会一起出现。

在给出以下示例数据的情况下,如何过滤掉例如将“ linda”和“ susy”组合在一起的行?

# 7 names
names <- c("joe", "mark", "mary", "john", "linda", "susy", "peter", "annie")

# All unique combinations of 5 names
cbn <- t(combn(names, 5))

实际数据要大得多,因此必须排除更多组合。理想情况下,我想找到一种简洁的方法来执行此操作,以便在需要更改排除项时必须尽可能少地调整代码。

2 个答案:

答案 0 :(得分:4)

这是一个带有两个参数的小函数:datax可以满足您的需求

f <- function(data, x) {
  data[rowSums(`dim<-`(data %in% x, dim(data))) < length(x), ]
}

该函数排除x中的值一起出现的行。

用法

x <- c("linda", "susy")
f(cbn, x)

结果

#      [,1]   [,2]   [,3]    [,4]    [,5]   
# [1,] "joe"  "mark" "mary"  "john"  "linda"
# [2,] "joe"  "mark" "mary"  "john"  "susy" 
# [3,] "joe"  "mark" "mary"  "john"  "peter"
# [4,] "joe"  "mark" "mary"  "john"  "annie"
# [5,] "joe"  "mark" "mary"  "linda" "peter"
# [6,] "joe"  "mark" "mary"  "linda" "annie"
# [7,] "joe"  "mark" "mary"  "susy"  "peter"
# [8,] "joe"  "mark" "mary"  "susy"  "annie"
# [9,] "joe"  "mark" "mary"  "peter" "annie"
#[10,] "joe"  "mark" "john"  "linda" "peter"
#[11,] "joe"  "mark" "john"  "linda" "annie"
#[12,] "joe"  "mark" "john"  "susy"  "peter"
#[13,] "joe"  "mark" "john"  "susy"  "annie"
#[14,] "joe"  "mark" "john"  "peter" "annie"
#[15,] "joe"  "mark" "linda" "peter" "annie"
#[16,] "joe"  "mark" "susy"  "peter" "annie"
#[17,] "joe"  "mary" "john"  "linda" "peter"
#[18,] "joe"  "mary" "john"  "linda" "annie"
#[19,] "joe"  "mary" "john"  "susy"  "peter"
#[20,] "joe"  "mary" "john"  "susy"  "annie"
#[21,] "joe"  "mary" "john"  "peter" "annie"
#[22,] "joe"  "mary" "linda" "peter" "annie"
#[23,] "joe"  "mary" "susy"  "peter" "annie"
#[24,] "joe"  "john" "linda" "peter" "annie"
#[25,] "joe"  "john" "susy"  "peter" "annie"
#[26,] "mark" "mary" "john"  "linda" "peter"
#[27,] "mark" "mary" "john"  "linda" "annie"
#[28,] "mark" "mary" "john"  "susy"  "peter"
#[29,] "mark" "mary" "john"  "susy"  "annie"
#[30,] "mark" "mary" "john"  "peter" "annie"
#[31,] "mark" "mary" "linda" "peter" "annie"
#[32,] "mark" "mary" "susy"  "peter" "annie"
#[33,] "mark" "john" "linda" "peter" "annie"
#[34,] "mark" "john" "susy"  "peter" "annie"
#[35,] "mary" "john" "linda" "peter" "annie"
#[36,] "mary" "john" "susy"  "peter" "annie"

当您必须反复检查多个组合时,可以使用for循环

x <- c("linda", "susy")
y <- c("joe", "john")

使用这些组合创建列表

combs <- list(x, y)

在循环中应用f(从上方)

for(i in 1:length(combs)) {
  cbn <- f(cbn, combs[[i]])
}

结果

cbn
#      [,1]   [,2]   [,3]    [,4]    [,5]   
# [1,] "joe"  "mark" "mary"  "linda" "peter"
# [2,] "joe"  "mark" "mary"  "linda" "annie"
# [3,] "joe"  "mark" "mary"  "susy"  "peter"
# [4,] "joe"  "mark" "mary"  "susy"  "annie"
# [5,] "joe"  "mark" "mary"  "peter" "annie"
# [6,] "joe"  "mark" "linda" "peter" "annie"
# [7,] "joe"  "mark" "susy"  "peter" "annie"
# [8,] "joe"  "mary" "linda" "peter" "annie"
# [9,] "joe"  "mary" "susy"  "peter" "annie"
#[10,] "mark" "mary" "john"  "linda" "peter"
#[11,] "mark" "mary" "john"  "linda" "annie"
#[12,] "mark" "mary" "john"  "susy"  "peter"
#[13,] "mark" "mary" "john"  "susy"  "annie"
#[14,] "mark" "mary" "john"  "peter" "annie"
#[15,] "mark" "mary" "linda" "peter" "annie"
#[16,] "mark" "mary" "susy"  "peter" "annie"
#[17,] "mark" "john" "linda" "peter" "annie"
#[18,] "mark" "john" "susy"  "peter" "annie"
#[19,] "mary" "john" "linda" "peter" "annie"
#[20,] "mary" "john" "susy"  "peter" "annie"

答案 1 :(得分:0)

另一种解决方案:

创建名称和组合

names <- c("joe", "mark", "mary", "john", "linda", "susy", "peter", "annie")
cbn <- combn(names, 5)

排除成对的行

csums <- colSums((cbn == "linda") + (cbn == "susy"))
csums_2 <- colSums((cbn == "joe") + (cbn == "john"))
cbn <- t(cbn[, csums < 2 & csums_2 <2])

结果:

> cbn
      [,1]   [,2]   [,3]    [,4]    [,5]   
 [1,] "joe"  "mark" "mary"  "linda" "peter"
 [2,] "joe"  "mark" "mary"  "linda" "annie"
 [3,] "joe"  "mark" "mary"  "susy"  "peter"
 [4,] "joe"  "mark" "mary"  "susy"  "annie"
 [5,] "joe"  "mark" "mary"  "peter" "annie"
 [6,] "joe"  "mark" "linda" "peter" "annie"
 [7,] "joe"  "mark" "susy"  "peter" "annie"
 [8,] "joe"  "mary" "linda" "peter" "annie"
 [9,] "joe"  "mary" "susy"  "peter" "annie"
[10,] "mark" "mary" "john"  "linda" "peter"
[11,] "mark" "mary" "john"  "linda" "annie"
[12,] "mark" "mary" "john"  "susy"  "peter"
[13,] "mark" "mary" "john"  "susy"  "annie"
[14,] "mark" "mary" "john"  "peter" "annie"
[15,] "mark" "mary" "linda" "peter" "annie"
[16,] "mark" "mary" "susy"  "peter" "annie"
[17,] "mark" "john" "linda" "peter" "annie"
[18,] "mark" "john" "susy"  "peter" "annie"
[19,] "mary" "john" "linda" "peter" "annie"
[20,] "mary" "john" "susy"  "peter" "annie"