[R] Get the location of a numeric element in a list
Martin Morgan
martin.morgan at roswellpark.org
Tue Jun 28 11:07:59 CEST 2016
On 06/28/2016 03:03 AM, Mohammad Tanvir Ahamed via R-help wrote:
> Can any one please help me. I will apply this for a very large list, about 400k vector in a list and vector size is unequal and large
>
> Example :
> Input:
> a <- c(1,3,6,9,25,100)
> b<-c(10,7,20,2,25)
> c<-c(1,7,5,15,25,300,1000)
> d<-list(a,b,c)
>
> Expected outcome :
> # When looking for 1 in d
> c(1,3)
>
> # When looking for 7 in d
>
> c(2,3)
>
> # when looking for 25 in d
> c(1,2,3)
> # When looking for 50 in d
> NULL or 0
Make a vector of queries
queries = c(1, 7, 25, 50)
Create a factor of unlist(d), using queries as levels. Create a vector
rep(seq_along(d), lengths(d)), and split it into groups defined by f
f = factor(unlist(d, use.names=FALSE), levels=queries)
split(rep(seq_along(d), lengths(d)), f)
Martin Morgan
>
>
> Thanks in advance !!
>
>
>
>
>
> Tanvir Ahamed
> Göteborg, Sweden | mashranga at yahoo.com
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
This email message may contain legally privileged and/or...{{dropped:2}}
More information about the R-help
mailing list