在数据框中查找包含字符串向量中一个元素的行的索引

Find the index of the row in data frame that contain one element in a string vector

如果我有这样的data.frame

df <- data.frame(col1 = c(letters[1:4],"a"),col2 = 1:5,col3 = letters[10:14])
 df
   col1 col2 col3
1    a    1    j
2    b    2    k
3    c    3    l
4    d    4    m
5    a    5    n

我想获取包含 c("a", "k", "n") 中的元素之一的行索引;在此示例中,结果应为 1, 2, 5.

s <- c('a','k','n');
which(df$col1%in%s|df$col3%in%s);
## [1] 1 2 5

这是另一个解决方案。这个适用于整个 data.frame,并且恰好将搜索字符串捕获为元素名称(您可以通过 unname() 摆脱这些):

sapply(s,function(s) which(apply(df==s,1,any))[1]);
## a k n
## 1 2 5

原方案二:

sort(unique(rep(1:nrow(df),ncol(df))[as.matrix(df)%in%s]));
## [1] 1 2 5

如果你有一个大数据框并且你想检查所有列,试试这个

x <- c("a", "k", "n")

Reduce(union, lapply(x, function(a) which(rowSums(df == a) > 0)))
# [1] 1 5 2

当然你可以对最终结果进行排序。