Hi Akshaka,
Take a look at ?"%in%". Here is an example for the help:

> x<-1:10
> key<-c(1,3,5,9)
> x %in% key
 [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
> x[ x %in% key ]
[1] 1 3 5 9


HTH,

Jorge



On Sun, Jan 25, 2009 at 10:27 PM, Akshaya Jha <akshayaj@andrew.cmu.edu>wrote:

> Hi,
>
> I have the following datasets:
> x=data I am looking through
> key=a set of data with the codes I want
>
> I have the following issue:
> I want the subset of x which has a code contained in the key dataset.  That
> is, if x[i] is contained in the key dataset, I want to keep it.  Note that x
> may contain multiple of the same codes (or obviously none of that code as
> well)
>
> I currently use two for-loops thusly in my R-code:
>
> k=1
> y=data.frame(1,stringsAsFactors=FALSE)
> for(i in 1:length(x)){
> for(j in 1:length(key)){
>
> if(x[i]==key[j]){
> y[k]=x[i]
> k=k+1;
> }
>
> }
> }
>
> However, my dataset (x in this example) is pretty large, so I want to avoid
> using two for-loops.  Does anybody know an easier way to approach this?
>
> Thanks
>
> ______________________________________________
> R-help@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.
>

	[[alternative HTML version deleted]]


