[R] Compare each element of a list to a vector

jim holtman jholtman at gmail.com
Sun Feb 3 19:24:39 CET 2013


try this:

> x<-c(1,2,3)
> datalist<-list(c(1,2,3),c(2,3,4),c(3,4,5),c(4,5,6))
>
> result <- sapply(datalist, function(.vec){
+     all(.vec == x)
+ })
>
> result
[1]  TRUE FALSE FALSE FALSE
>


On Sun, Feb 3, 2013 at 1:15 PM,  <mtb954 at gmail.com> wrote:
> Hello R-helpers,
>
> I have a vector
>
> x<-c(1,2,3)
>
> and a list that contains vectors
>
> datalist<-list(c(1,2,3),c(2,3,4),c(3,4,5),c(4,5,6))
>
> and I would like to identify those list elements that are identical to x.
>
> I tried
>
>> datalist %in% x
> [1] FALSE FALSE FALSE FALSE
>
> but I am obviously using %in% incorrectly. I also tried messing around with
> lapply but I can't figure out how to specify the function within lapply.
>
> I would appreciate any suggestions you may have.
>
> Many thanks!
>
> Mark Na
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list