The goal is to be able to use a "subset expression" (not a logical vector) as an (optional) argument to a user-defined function and use that to compute the subset of a data-frame.
x <- data.frame(a=1, b=gl(2,5))
f <- function(data, subset) {
if (!missing(subset))
data <- subset(data, subset)
data
}
The code above doesn't work, and neither does
f <- function(data, subset) {
if (!missing(subset))
data <- data[with(data, subset), ]
data
}
In both cases I get an error when subset is supplied.
> f(x, b == 2)
Error in f(x, b == 2) (from frame.r!322341dM#2) : object 'b' not found
Desired output:
> f(x)
a b
1 1 1
2 1 1
3 1 1
4 1 1
5 1 1
6 1 2
7 1 2
8 1 2
9 1 2
10 1 2
> f(x, b == 2)
a b
6 1 2
7 1 2
8 1 2
9 1 2
10 1 2