Dear all, I'd like to subset a df within a function, and use select for choosing the variable. Something like (simplified example): mydf <- data.frame(a= 0:9, b= 10:19) ttt <- function(vv) { tmpdf <- subset(mydf, select= vv) mean(tmpdf$vv) } ttt(mydf$b) But this is not the correct way. Any help? Thanks in advance Juli
Juli, Check again in the help about how to use the select parameter. You need to pass the column name for the column of interest, not the actual column. Also, your tmpdf will already have the column selected, so there is no need to index the column further, at least in this toy example. In this case, I think you could use something like:> ttfunction(vv) { tmpdf <- subset(mydf,select=vv) mean(tmpdf) }> tt("b")b 14.5 Sean On 1/21/04 6:56 AM, "juli g. pausas" <juli at ceam.es> wrote:> Dear all, > I'd like to subset a df within a function, and use select for choosing > the variable. Something like (simplified example): > > mydf <- data.frame(a= 0:9, b= 10:19) > > ttt <- function(vv) { > tmpdf <- subset(mydf, select= vv) > mean(tmpdf$vv) > } > > ttt(mydf$b) > > But this is not the correct way. Any help? > Thanks in advance > > Juli > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
juli g. pausas wrote:> Dear all, > I'd like to subset a df within a function, and use select for choosing > the variable. Something like (simplified example): > > mydf <- data.frame(a= 0:9, b= 10:19) > > ttt <- function(vv) { > tmpdf <- subset(mydf, select= vv) > mean(tmpdf$vv) > } > > ttt(mydf$b) > > But this is not the correct way. Any help? > Thanks in advance > > Juli > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlare you looking for something like: mydf <- data.frame(a= 0:9, b= 10:19) ttt <- function(vv) { tmpdf <- subset(mydf, select= vv) lapply(tmpdf,mean) } ttt("b") $b [1] 14.5 > ttt(c("a","b")) $a [1] 4.5 $b [1] 14.5 ?? Peter