On Jan 30, 2008 1:15 PM, Ted Harding <[EMAIL PROTECTED]> wrote: > On 30-Jan-08 19:47:55, Ramon Hidalgo wrote: > > Hello, > > > > How can I make the following expressions are equivalent > > datos$Col1 and datos$var when I define var <- "Col1"? > > > > I am trying to get the same result with > >> datos$Col1 > > [1] 0 1 1 0 1 0 1 1 0 > > > > And > >> datos$var > > NULL
This one must the most frequently asked question among all FAQs. > > It will work if you use [], though I'm not sure that > this is the best or orthodox way to do it: > > datos<-data.frame(Col1=c(0,1,1,0,1,0,1,1,0),Col2=c(1,2,2,1,2,1,2,2,1)) > var<-"Col1" > datos[var] > ## Col1 > ##1 0 > ##2 1 > ##3 1 > ##4 0 > ##5 1 > ##6 0 > ##7 1 > ##8 1 > ##9 0 In order to get a similar return value as operator $() you want to use [[(), i.e. datos[[var]] or alternatively datos[,var] In the first case you treat the data frame as if it is a list structure, and in the second case you treat it as if it is a matrix, which will drop the dimensions if you ask for a single column, cf. datos[,var] datos[,var, drop=TRUE] datos[,var, drop=FALSE] /Henrik > > Hoping this helps, > Ted. > > -------------------------------------------------------------------- > E-Mail: (Ted Harding) <[EMAIL PROTECTED]> > Fax-to-email: +44 (0)870 094 0861 > Date: 30-Jan-08 Time: 21:15:07 > ------------------------------ XFMail ------------------------------ > > ______________________________________________ > R-help@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. > ______________________________________________ R-help@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.