Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making `subset()` work in a function

Tags:

r

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
like image 463
Ernest A Avatar asked Nov 24 '25 20:11

Ernest A


1 Answers

This one seems work. You have to tell f that second arg is an expression:

f <- function(data, ss) {
    if (!missing(ss)){
        e <- substitute(ss)
        r <- eval(e, data, parent.frame())
        data <- subset(data, r)
    }
    data
}

# > f(x, b == 2)
#    a b
# 6  1 2
# 7  1 2
# 8  1 2
# 9  1 2
# 10 1 2

replace argument name ss with subset won't work. I am not sure why. Actually, I came up with by navigating source code of subset.data.frame.

like image 68
mt1022 Avatar answered Nov 27 '25 09:11

mt1022



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!