how do you subset R data frame based on provided column names via a function argument? -
i trying create r function based on given data frame first argument, subset data frame based on provided arguments:
for example, df this:
date server1 server2 server3 server4 1/1/2004 10 20 10 5 2/1/2014 4 4 4 20 3/2/2014 1 5 5 39
i need subset df:
for example, if pass function(x, server1, server3, server4), this:
data<-function(x, ...) { subset(x, select=c("server1","server3", "server4")) }
but, argument list should not known. should apply data frames not knowing column names.
any ideas how accomplish in r?
if pass arguments strings should work fine this:
subset2<-function(x, ...) { cols <- c(...) subset(x, select=cols) } subset2(dat, "server1", "server3", "server4")
but i'm not sure why such wrapper necessary. perhaps missing true goal is?
Comments
Post a Comment