[R] subset with variable number of subset arguments

From: Brar Piening <Brar_at_gmx.de>
Date: Fri, 31 Oct 2008 16:18:11 +0100


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.
Received on Fri 31 Oct 2008 - 15:21:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 31 Oct 2008 - 16:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive