在R中搜索列表条目的索引 [英] Search for index of a list entry in R
本文介绍了在R中搜索列表条目的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定一个R列表,我希望找到给定列表条目的索引.例如,对于条目"36"
,我希望输出为"2"
.另外,如何使用lapply并行执行此类查询?
Given an R list, I wish to find the index of a given list entry. For example, for entry "36"
, I want my output to be "2"
. Also, how could I do such queries in parallel using lapply?
> list
$`1`
[1] "7" "12" "26" "29"
$`2`
[1] "11" "36"
$`3`
[1] "20" "49"
$`4`
[1] "39" "41"
推荐答案
这是一种单行代码,它允许(可能吗?)列表中的多个元素可能包含您要搜索的字符串:
Here's a one-liner that allows for the (likely?) possibility that more than one element of the list will contain the string for which you're searching:
## Some example data
ll <- list(1:4, 5:6, 7:12, 1:12)
ll <- lapply(ll, as.character)
which(sapply(ll, FUN=function(X) "12" %in% X))
# [1] 3 4
这篇关于在R中搜索列表条目的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文