[R] subset with variable number of subset arguments
Brar Piening
Brar at gmx.de
Fri Oct 31 16:18:11 CET 2008
I've got a big data.frame from which I need to extract data based on a variable number of Id's (InfCode).
Until now I've been using the following dull solution as I never needed to search for more than 5 codes.
Now my needs have increased faster than my R skills did and I need to call my function with about 25 values for x.
There has to be a *apply or even simpler solution which (after RTM) I'm not able to figure out.
Can anybody direct me to a possible solution?
Regards,
Brar
GetInfCodeSubset <- function(set, x){
if(1 == length(x))
return(subset(set, (set$InfCode == x[1])))
else if(2 == length(x))
return(subset(set, (set$InfCode == x[1]) | (set$InfCode == x[2])))
else if(3 == length(x))
return(subset(set, (set$InfCode == x[1]) | (set$InfCode == x[2]) | (set$InfCode == x[3])))
else if(4 == length(x))
return(subset(set, (set$InfCode == x[1]) | (set$InfCode == x[2]) | (set$InfCode == x[3]) | (set$InfCode == x[4])))
else if(5 == length(x))
return(subset(set, (set$InfCode == x[1]) | (set$InfCode == x[2]) | (set$InfCode == x[3]) | (set$InfCode == x[4]) | (set$InfCode == x[5])))
else if(6 == length(x))
return(subset(set, (set$InfCode == x[1]) | (set$InfCode == x[2]) | (set$InfCode == x[3]) | (set$InfCode == x[4]) | (set$InfCode == x[5]) | (set$InfCode == x[6])))
else stop("Too many elements in x")
}
set <- data.frame(PatId = c(1:100), InfCode = rep(c(1:20), 5))
GetInfCodeSubset(set, c(1,3,9))
--
More information about the R-help
mailing list