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

2012-08-22 Thread Duncan Murdoch
On 12-08-18 12:33 PM, Martin Maechler 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 column to a 'row' vector and

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

2012-08-22 Thread Joshua Ulrich
On Tue, Aug 21, 2012 at 2:34 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 12-08-18 12:33 PM, Martin Maechler 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

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

2012-08-19 Thread Bert Gunter
Yes, but either drop(t(df[,1,drop=TRUE])) or t(df[,1,drop=TRUE])[1,] does work. My minimal effort to check timings found that the first version was a hair faster. -- Bert On Sat, Aug 18, 2012 at 9:01 AM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, A bit more general nv - c(a=1,

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

2012-08-19 Thread Bert Gunter
Or to expand just a hair on Joshua's suggestion, is the following what you want: x - 1:10 names(x) - letters[1:10] x a b c d e f g h i j 1 2 3 4 5 6 7 8 9 10 df - data.frame(x=x,y=LETTERS[1:10],row.names=names(x)) df x y a 1 A b 2 B c 3 C d 4 D e 5 E f 6 F g 7 G

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

2012-08-19 Thread Bert Gunter
Sorry! -- Change that to drop = FALSE ! drop(t(df[,1,drop=FALSE])) t(df[,1,drop=FALSE])[1,] -- Bert On Sat, Aug 18, 2012 at 9:37 AM, Bert Gunter bgun...@gene.com wrote: Yes, but either drop(t(df[,1,drop=TRUE])) or t(df[,1,drop=TRUE])[1,] does work. My minimal effort to check timings

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

2012-08-19 Thread J. R. M. Hosking
On 2012-08-18 11:03, Martin Maechler 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 the quiz, I know one quite cute/slick

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

2012-08-19 Thread Andrew Piskorski
On Sat, Aug 18, 2012 at 02:13:20PM -0400, Christian Brechb?hler wrote: 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:

[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,

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 -