Let's say I have the data frame 'dd' below. I'd like to select one column from this data frame (say 'a') and keep its name in the resulting data frame. That can be done as in #2. However, what if I want to make my selection based on a vector of names (and again keep those names in the resulting data frame). My attempt is #4 but doesn't work. dd <- data.frame(a = gl(3,4), b = gl(4,1,12), c=rnorm(12)) #1 data.frame("a" = dd[,"a"]) #2 mynames <- "a" #3 data.frame(eval(mynames) = dd[, mynames]) #4 thanks, Lars.
dd[[ myname]] Sent from my iPad On May 21, 2011, at 7:37, Lars Bishop <lars52r at gmail.com> wrote:> Let's say I have the data frame 'dd' below. I'd like to select one > column from this data frame (say 'a') and keep its name in the > resulting data frame. That can be done as in #2. However, what if I > want to make my selection based on a vector of names (and again keep > those names in the resulting data frame). My attempt is #4 but doesn't > work. > > dd <- data.frame(a = gl(3,4), b = gl(4,1,12), c=rnorm(12)) #1 > data.frame("a" = dd[,"a"]) #2 > > mynames <- "a" #3 > data.frame(eval(mynames) = dd[, mynames]) #4 > > thanks, > Lars. > > ______________________________________________ > 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.
Are you looking for: dd[, "a", drop=FALSE] On 21/05/2011 12:37, Lars Bishop wrote:> Let's say I have the data frame 'dd' below. I'd like to select one > column from this data frame (say 'a') and keep its name in the > resulting data frame. That can be done as in #2. However, what if I > want to make my selection based on a vector of names (and again keep > those names in the resulting data frame). My attempt is #4 but doesn't > work. > > dd<- data.frame(a = gl(3,4), b = gl(4,1,12), c=rnorm(12)) #1 > data.frame("a" = dd[,"a"]) #2 > > mynames<- "a" #3 > data.frame(eval(mynames) = dd[, mynames]) #4 > > thanks, > Lars. > > ______________________________________________ > 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. >-- Patrick Burns pburns at pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')
On May 21, 2011, at 7:37 AM, Lars Bishop wrote:> Let's say I have the data frame 'dd' below. I'd like to select one > column from this data frame (say 'a') and keep its name in the > resulting data frame. That can be done as in #2. However, what if I > want to make my selection based on a vector of names (and again keep > those names in the resulting data frame). My attempt is #4 but doesn't > work. > > dd <- data.frame(a = gl(3,4), b = gl(4,1,12), c=rnorm(12)) #1 > data.frame("a" = dd[,"a"]) #2 > > mynames <- "a" #3 > data.frame(eval(mynames) = dd[, mynames]) #4dd[mynames] (Will succeed with a vector of names as well.)> > thanks, > Lars. > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT