[Rd] Quiz: How to get a named column from a data frame

2012-08-18 Thread Martin Maechler
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

Re: [Rd] Quiz: How to get a named column from a data frame

2012-08-18 Thread Joshua Ulrich
I don't know if this is better, but it's the most obvious/shortest I could come up with. Transpose the data.frame column to a 'row' vector and drop the dimensions. R identical(nv, drop(t(df))) [1] TRUE Best, -- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com On

Re: [Rd] Quiz: How to get a named column from a data frame

2012-08-18 Thread Rui Barradas
Hello, A bit more general nv - c(a=1, d=17, e=101); nv nv2 - c(a=a, d=d, e=e) df2 - data.frame(VAR = nv, CHAR = nv2); df2 identical( nv, drop(t( df2[1] )) ) # TRUE identical( nv, drop(t( df2[[1]] )) ) # FALSE Rui Barradas Em 18-08-2012 16:16, Joshua Ulrich escreveu: I don't know if this

Re: [Rd] Quiz: How to get a named column from a data frame

2012-08-18 Thread Martin Maechler
On Sat, Aug 18, 2012 at 5:14 PM, Christian Brechbühler wrote: On Sat, Aug 18, 2012 at 11:03 AM, Martin Maechler maech...@stat.math.ethz.ch wrote: 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,

[Rd] mccollect does not return named, ordered results when wait=FALSE

2012-08-18 Thread Martin Morgan
?mccollect say 'mccollect' returns any results that are available in a list. The results will have the same order as the specified jobs. If there are multiple jobs and a job has a name it will be used to name the result, otherwise its process ID will be used. If none of

Re: [Rd] Quiz: How to get a named column from a data frame

2012-08-18 Thread Martin Maechler
Joshua Ulrich josh.m.ulr...@gmail.com on Sat, 18 Aug 2012 10:16:09 -0500 writes: I don't know if this is better, but it's the most obvious/shortest I could come up with. Transpose the data.frame column to a 'row' vector and drop the dimensions. R identical(nv,

Re: [Rd] Quiz: How to get a named column from a data frame

2012-08-18 Thread Joshua Wiley
On Sat, Aug 18, 2012 at 9:33 AM, Martin Maechler maech...@stat.math.ethz.ch wrote: Joshua Ulrich josh.m.ulr...@gmail.com on Sat, 18 Aug 2012 10:16:09 -0500 writes: I don't know if this is better, but it's the most obvious/shortest I could come up with. Transpose the data.frame

Re: [Rd] Quiz: How to get a named column from a data frame

2012-08-18 Thread Christian Brechbühler
On 8/18/12, Martin Maechler maech...@stat.math.ethz.ch wrote: On Sat, Aug 18, 2012 at 5:14 PM, Christian Brechbühler wrote: On Sat, Aug 18, 2012 at 11:03 AM, Martin Maechler maech...@stat.math.ethz.ch wrote: Consider this toy example, where the dataframe already has only one column :

Re: [Rd] Quiz: How to get a named column from a data frame

2012-08-18 Thread Winston Chang
This isn't super-concise, but has the virtue of being clear: nv - c(a=1, d=17, e=101) df - as.data.frame(cbind(VAR = nv)) identical(nv, setNames(df$VAR, rownames(df))) # TRUE It seems to be more efficient than the other methods as well: f1 - function() setNames(df$VAR, rownames(df)) f2 -

Re: [Rd] Quiz: How to get a named column from a data frame

2012-08-18 Thread Hadley Wickham
On Sat, Aug 18, 2012 at 10:03 AM, Martin Maechler maech...@stat.math.ethz.ch wrote: 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

Re: [Rd] Quiz: How to get a named column from a data frame

2012-08-18 Thread William Dunlap
That would have been essentially my suggestion as well. I prefer its clarity (and speed). I didn't know if you wanted your solution to also apply to matrices embedded in data.frames. In S+ rownames-() works on vectors (because it calls the generic rowId-()) so the following works: f4 -