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 wrote: > On 12-08-18 12:33 PM, Martin Maechler wrote: >>> >>> Joshua Ulrich >>> 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

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 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.

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 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 > >> wrote: > > >>> Consider this toy example, where the dataf

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" an

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 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 found t

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

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 wrote: > Hello, > > A bit more general > > nv <- c(a=1, d=17, e=101); nv >

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

2012-08-18 Thread William Dunlap
project.org [mailto:r-devel-boun...@r-project.org] On > Behalf > Of Winston Chang > Sent: Saturday, August 18, 2012 11:54 AM > To: Martin Maechler > Cc: R. Devel List > Subject: Re: [Rd] Quiz: How to get a "named column" from a data frame > > This isn't super-

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 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 qu

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 <- f

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 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 >> wrote: >>> Consider this toy example, where the dataframe already has only >>> one column : >>> >>> > nv <- c(a=1, d=17, e=101); 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 wrote: >> Joshua Ulrich >> 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 > >

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

2012-08-18 Thread Martin Maechler
> Joshua Ulrich > 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, drop(t(df))) >

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 > 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 mu

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 i

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 Sa

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