list 在 R 中查找列表元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17358289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding Elements of Lists in R
提问by riders994
Right now I'm working with a character vector in R, that i use strsplit to separate word by word. I'm wondering if there's a function that I can use to check the whole list, and see if a specific word is in the list, and (if possible) say which elements of the list it is in.
现在我正在使用 R 中的字符向量,我使用 strsplit 逐字分隔。我想知道是否有一个函数可以用来检查整个列表,看看列表中是否有特定的单词,并且(如果可能)说出它在列表中的哪些元素。
ex.
前任。
a = c("a","b","c")
b= c("b","d","e")
c = c("a","e","f")
If z=list(a,b,c)
, then f("a",z)
would optimally yield [1] 1 3
, and f("b",z)
would optimally yield [1] 1 2
如果z=list(a,b,c)
,那么f("a",z)
将最优产生[1] 1 3
,并且f("b",z)
将最优产生[1] 1 2
Any assistance would be wonderful.
任何帮助都会很棒。
回答by Hong Ooi
As alexwhan says, grep
is the function to use. However, be carefulabout using it with a list. It isn't doing what you might think it's doing. For example:
正如alexwhan所说,grep
是要使用的功能。但是,将它与列表一起使用时要小心。它没有做你认为它在做的事情。例如:
grep("c", z)
[1] 1 2 3 # ?
grep(",", z)
[1] 1 2 3 # ???
What's happening behind the scenes is that grep
coerces its 2nd argument to character, using as.character
. When applied to a list, what as.character
returns is the character representation of that list as obtained by deparsing it. (Modulo an unlist.)
幕后发生的事情是将grep
其第二个参数强制转换为字符,使用as.character
. 当应用于列表时,as.character
返回的是通过对它进行解析获得的该列表的字符表示。(模一个取消列表。)
as.character(z)
[1] "c(\"a\", \"b\", \"c\")" "c(\"b\", \"d\", \"e\")" "c(\"a\", \"e\", \"f\")"
cat(as.character(z))
c("a", "b", "c") c("b", "d", "e") c("a", "e", "f")
This is what grep
is working on.
这就是grep
正在进行的工作。
If you want to run grep
on a list, a safer method is to use lapply
. This returns another list, which you can operate on to extract what you're interested in.
如果要grep
在列表上运行,更安全的方法是使用lapply
. 这将返回另一个列表,您可以对其进行操作以提取您感兴趣的内容。
res <- lapply(z, function(ch) grep("a", ch))
res
[[1]]
[1] 1
[[2]]
integer(0)
[[3]]
[1] 1
# which vectors contain a search term
sapply(res, function(x) length(x) > 0)
[1] TRUE FALSE TRUE
回答by Adam Waring
Much faster than grep is:
比 grep 快得多的是:
sapply(x, function(y) x %in% y)
and if you want the index of course just use which():
如果你想要索引当然只是使用 which():
which(sapply(x, function(y) x %in% y))
Evidence!
证据!
x = setNames(replicate(26, list(sample(LETTERS, 10, rep=T))), sapply(LETTERS, list))
head(x)
$A
[1] "A" "M" "B" "X" "B" "J" "P" "L" "M" "L"
$B
[1] "H" "G" "F" "R" "B" "E" "D" "I" "L" "R"
$C
[1] "P" "R" "C" "N" "K" "E" "R" "S" "N" "P"
$D
[1] "F" "B" "B" "Z" "E" "Y" "J" "R" "H" "P"
$E
[1] "O" "P" "E" "X" "S" "Q" "S" "A" "H" "B"
$F
[1] "Y" "P" "T" "T" "P" "N" "K" "P" "G" "P"
system.time(replicate(1000, grep("A", x)))
user system elapsed
0.11 0.00 0.11
system.time(replicate(1000, sapply(x, function(y) "A" %in% y)))
user system elapsed
0.05 0.00 0.05
回答by alexwhan
You're looking for grep()
:
您正在寻找grep()
:
grep("a", z)
#[1] 1 3
grep("b", z)
#[1] 1 2