Skip to content
Back to formatted view

Raw Message

Message-ID: <E66794E69CFDE04D9A70842786030B931B8FD205@PA-MBX04.na.tibco.com>
Date: 2013-02-03T18:32:35Z
From: William Dunlap
Subject: Compare each element of a list to a vector
In-Reply-To: <CAOF_sWTAz1zGwJvhk-BO1WT0Yqx5yzxkj+Y1R==1qPvQNRWx-A@mail.gmail.com>

Try
  > datalist %in% list(x)
  [1]  TRUE FALSE FALSE FALSE
Both arguments, e1 and e2, of e1 %in% e2 should be of the same type:
e1 %in% e2 is comparing e1[i] and e2[j].

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of mtb954 at gmail.com
> Sent: Sunday, February 03, 2013 10:15 AM
> To: r-help at r-project.org
> Subject: [R] Compare each element of a list to a vector
> 
> 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.