[R] a replace for subset

Jim Lemon drjimlemon at gmail.com
Fri Apr 15 13:00:18 CEST 2016


Hi Elahe,
When you want to include a usable toy data frame, it's better to use
something like:

dput(mydata[1:100])

So if we have a data frame like this:

mydata<-data.frame(RE=sample(5:50,100,TRUE),
  LU=sample(1500:4500,100),
  COUNTRY=factor(sample(c("DE","FR","JP","AU"),100,TRUE)),
  Light=factor(sample(c("ON","OFF"),100,TRUE)),
  OR=factor(sample(c("S","T"),100,TRUE)),
  PAT=factor(sample(c("low","high","middle"),100,TRUE)))

Then you can create logical expressions for all combinations of your
levels like this:

subcomb<-expand.grid(list(levels(mydata$COUNTRY),
 levels(mydata$Light),levels(mydata$OR),levels(mydata$PAT)))

Then you can loop through this data frame creating a string that
corresponds to your logical expression:

for(subsetter in 1:dim(subcomb)[1]) {
 subexpr<-paste(names(mydata)[3:6],subcomb[subsetter,],
  sep="==",collapse="&")
 subset(mydata,do_something_to(subexpr),select=c("RE","LU"))
}

but I have not been able to work out how to transform the strings
"subexpr" into logical expressions. I feel pretty sure that someone
will show me up on this.

Jim



More information about the R-help mailing list