[R] subset with variable number of subset arguments
Brar Piening
brar at gmx.de
Fri Oct 31 20:17:05 CET 2008
Thank you very much!
Brar
Erik Iverson schrieb:
> I think
>
> subset(set, InfCode %in% x)
>
>
> does it, where 'x' is your vector of ids (e.g., c(1,3,9) in your
> example at the bottom)
>
> Brar Piening wrote:
>> 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))
>>
>> --
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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