[R] selecting rows based on multiple criteria

arun smartpink111 at yahoo.com
Wed May 1 15:25:45 CEST 2013


You could also use: 

which(apply(coord,1,paste,collapse="")%in%apply(choice,1,paste,collapse=""))
#[1]  2 11 12

#or
which(sapply(seq_len(nrow(coord)),function(i) any(duplicated(rbind(coord[i,],choice)))))
#[1]  2 11 12
#or
 coord$Newcol1<- TRUE
 choice$Newcol2<- TRUE
 which(!is.na(merge(coord,choice,all=TRUE)[,4]))
#[1]  2 11 12
A.K.



----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Enrico R. Crema <enryu_crema at yahoo.it>
Cc: R help <r-help at r-project.org>
Sent: Wednesday, May 1, 2013 9:09 AM
Subject: Re: [R] selecting rows based on multiple criteria

 match(interaction(choice),interaction(coord))
#[1]  2 11 12
A.K.



----- Original Message -----
From: Enrico R. Crema <enryu_crema at yahoo.it>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, May 1, 2013 7:54 AM
Subject: [R] selecting rows based on multiple criteria

Dear List, 

I am struggling with the following problem. 

Suppose I have the following data.frame:

coord<-expand.grid(x=1:10,y=1:10)

and I want to extract the row numbers of those matching the criteria defined by the following data.frame:

choice<-data.frame(x=c(2,1,2),y=c(1,2,2))

the result should return in this case:

2,11,12

All I managed to do is the following:

which(data coord$x%in%choice$x & coord$y%in%choice$y);

which returns c(1,2,11,12)... which includes also the first row... 

Any thoughts?

Enrico
______________________________________________
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.




More information about the R-help mailing list