[Rd] Quiz: How to get a "named column" from a data frame
maechler at stat.math.ethz.ch
Sat Aug 18 17:03:28 CEST 2012
Today, I was looking for an elegant (and efficient) way
to get a named (atomic) vector by selecting one column of a data frame.
Of course, the vector names must be the rownames of the data frame.
Ok, here is the quiz, I know one quite "cute"/"slick" answer, but was
wondering if there are obvious better ones, and
also if this should not become more idiomatic (hence "R-devel"):
Consider this toy example, where the dataframe already has only
one column :
> nv <- c(a=1, d=17, e=101); nv
a d e
1 17 101
> df <- as.data.frame(cbind(VAR = nv)); df
Now how, can I get 'nv' back from 'df' ? I.e., how to get
> identical(nv, .......)
where ...... only uses 'df' (and no non-standard R packages)?
As said, I know a simple solution (*), but I'm sure it is not
obvious to most R users and probably not even to the majority of
R-devel readers... OTOH, people like Bill Dunlap will not take
long to provide it or a better one.
(*) In my solution, the above '.......' consists of 17 letters.
I'll post it later today (CEST time) ... or confirm
that someone else has done so.
More information about the R-devel