Julia Burggraaf
2012-Jan-13 22:34 UTC
[R] question: how to select a column from a dataframe in a function
Hi, I am creating a function and ran into the problem of selecting a column from a dataset. It seems as though the $ function (as in data$columnname) does not apply in the function. In simplified version: This works: testf2<-function(data,columnnumber){print(data[,columnnumber])} But this doesn't: testf<-function(data,column){print(data$column)} Even though the first solution works, I would like to be able to insert the columnname in the function, instead of the columnnumber. How do I do that? Thank you in advance, Julia [[alternative HTML version deleted]]
R. Michael Weylandt
2012-Jan-14 01:47 UTC
[R] question: how to select a column from a dataframe in a function
Just put the columnname variable into the brackets as well: they accept strings. As a general rule, always use the brackets (single or double) when programming: `$` has some great features for interactive use but can bite if you're not careful. Michael On Fri, Jan 13, 2012 at 5:34 PM, Julia Burggraaf <jmburggraaf at gmail.com> wrote:> Hi, > > I am creating a function and ran into the problem of selecting a column > from a dataset. It seems as though the $ function (as in data$columnname) > does not apply in the function. In simplified version: > > This works: > ?testf2<-function(data,columnnumber){print(data[,columnnumber])} > > But this doesn't: > testf<-function(data,column){print(data$column)} > > Even though the first solution works, I would like to be able to insert the > columnname in the function, instead of the columnnumber. How do I do that? > > Thank you in advance, > > Julia > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Petr PIKAL
2012-Jan-17 14:37 UTC
[R] question: how to select a column from a dataframe in a function
Hi> > Hi, > > I am creating a function and ran into the problem of selecting a column > from a dataset. It seems as though the $ function (as indata$columnname)> does not apply in the function. In simplified version: > > This works: > testf2<-function(data,columnnumber){print(data[,columnnumber])} > > But this doesn't: > testf<-function(data,column){print(data$column)} > > Even though the first solution works, I would like to be able to insertthe> columnname in the function, instead of the columnnumber. How do I dothat? Not sure if you get any answer yet. testf2<-function(data,columnname){print(data[,columnname])} Petr> > Thank you in advance, > > Julia > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.