Reputation: 1034
I'm using the subset function in R and I'm wondering if there's a way for me to define the column name as a variable, and then subset that column name?
For example, right now I have something like this:
data.subset <- subset(df, age >= n1 & age < n2)
But say I wanted to subset on height instead of age. I want to have a variable that I can just set to "height"
and the subset will subset on height instead. I'm doing this to have a function that is adaptable. Something like this:
my.column <- "height"
data.subset <- subset(df, my.column >= n1 & my.column < n2)
Upvotes: 2
Views: 460
Reputation: 226162
If you insist on using subset
you can mess around with eval(parse(text=paste(...)))
(ugh!) but given what you're trying to do, subset
will be more trouble than it's worth.
my.column <- "height"
data.subset <- df[df[[my.column]] >= n1 & df[[my.column]] < n2),]
You could make this a little bit more compact by defining a between
function:
between <- function(x,val1,val2) x>=val1 & x < val2
df[between(df[[my.column]],n1,n2),]
Upvotes: 4