# [R] subset with variable number of subset arguments

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

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)))
else if(2 == length(x))
return(subset(set, (set\$InfCode == x) | (set\$InfCode == x)))
else if(3 == length(x))
return(subset(set, (set\$InfCode == x) | (set\$InfCode == x) | (set\$InfCode == x)))
else if(4 == length(x))
return(subset(set, (set\$InfCode == x) | (set\$InfCode == x) | (set\$InfCode == x) | (set\$InfCode == x)))
else if(5 == length(x))
return(subset(set, (set\$InfCode == x) | (set\$InfCode == x) | (set\$InfCode == x) | (set\$InfCode == x) | (set\$InfCode == x)))
else if(6 == length(x))
return(subset(set, (set\$InfCode == x) | (set\$InfCode == x) | (set\$InfCode == x) | (set\$InfCode == x) | (set\$InfCode == x) | (set\$InfCode == x)))
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