[R] Odp: subsetting data frame using a vector of column names / values

Petr PIKAL petr.pikal at precheza.cz
Wed Jun 24 15:06:50 CEST 2009


Hi

r-help-bounces at r-project.org napsal dne 24.06.2009 14:16:15:

> Hello
> 
> I have a data frame d with columns "var1", "var2", "var3"
> 
> Then I have two vectors:
> columns <- c("var2", "var3")
> values <- c(0, 1)
> 
> 
> Is there a compact way to subset the data frame
> using these two vectors and get the result equivalent to:
> 
> select (d, var2==0 & var3==1)  ?

The first part, to select columns seems to be straightforward

d[,columns]

the second part is a bit trickier e.g.

ind = which(rowSums(mapply("%in%", d[,columns], values))>1)
d[ind, columns]

shall give you the result

Regards
Petr


> 
> Thank You
> 
> B....j
> 
> ______________________________________________
> 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