Dear All, Probably a one liner, but I am banging my head against the floor. Consider the following DF <- data.frame( x=1:10, y=10:1, z=rep(5,10), a=11:20 ) mn<-names(DF) but then I cannot retrieve a column by doing e.g, DF$mn[2] I tried to play with the quotes and so on, but so far with no avail. Any suggestion is welcome. Cheers Lorenzo
On Feb 9, 2013, at 1:40 PM, Lorenzo Isella <lorenzo.isella at gmail.com> wrote:> Dear All, > Probably a one liner, but I am banging my head against the floor. > Consider the following > > DF <- data.frame( > x=1:10, > y=10:1, > z=rep(5,10), > a=11:20 > ) > > mn<-names(DF) > > but then I cannot retrieve a column by doing e.g, > > DF$mn[2] >$ doesn't evaluate it's arguments so I think you want something like: DF[[mn[2]]] or DF[,mn[2]] Cheers, MW> I tried to play with the quotes and so on, but so far with no avail. > Any suggestion is welcome. > Cheers > > Lorenzo > > ______________________________________________ > 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.
I don't know if I understood your problem, but maybe you can retrieve your columns simply in this way:> DF$y[1] 10 9 8 7 6 5 4 3 2 1 or (for looking at column)> DF[2]y 1 10 2 9 3 8 4 7 5 6 6 5 7 4 8 3 9 2 10 1 Bye D. 2013/2/9 Lorenzo Isella <lorenzo.isella at gmail.com>:> Dear All, > Probably a one liner, but I am banging my head against the floor. > Consider the following > > DF <- data.frame( > x=1:10, > y=10:1, > z=rep(5,10), > a=11:20 > ) > > mn<-names(DF) > > but then I cannot retrieve a column by doing e.g, > > DF$mn[2] > > I tried to play with the quotes and so on, but so far with no avail. > Any suggestion is welcome. > Cheers > > Lorenzo > > ______________________________________________ > 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.
Try DF [[mn [2]]] Sent from my Verizon Wireless 4G LTE Smartphone -------- Original message -------- From: Lorenzo Isella <lorenzo.isella@gmail.com> Date: 02/09/2013 8:40 AM (GMT-05:00) To: r-help@stat.math.ethz.ch Subject: [R] Addressing Columns in a Data Frame Dear All, Probably a one liner, but I am banging my head against the floor. Consider the following DF <- data.frame( x=1:10, y=10:1, z=rep(5,10), a=11:20 ) mn<-names(DF) but then I cannot retrieve a column by doing e.g, DF$mn[2] I tried to play with the quotes and so on, but so far with no avail. Any suggestion is welcome. Cheers Lorenzo ______________________________________________ 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. [[alternative HTML version deleted]]