From gunter.berton at gene.com  Sat Aug 18 18:37:19 2012
From: gunter.berton at gene.com (Bert Gunter)
Date: Sat, 18 Aug 2012 09:37:19 -0700
Subject: [Rd] Quiz: How to get a "named column" from a data frame
In-Reply-To: <502FBC4E.50800@sapo.pt>
References: <20527.44736.486888.854733@stat.math.ethz.ch>
	<CAPPM_gRmc+VE3ZGJ3TzTn6m4qb4ToUq30X0USATmmEjxq3u4dA@mail.gmail.com>
	<502FBC4E.50800@sapo.pt>
Message-ID: <CACk-te3LQC=nGoPdLDq-_oi1xz8-9HXRprd2=pjUOn8Uy5h+0A@mail.gmail.com>

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 <ruipbarradas at sapo.pt> wrote:
> 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 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 Sat, Aug 18, 2012 at 10:03 AM, Martin Maechler
>> <maechler at 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 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
>>>
>>>    VAR
>>> a   1
>>> d  17
>>> e 101
>>>
>>> Now how, can I get 'nv' back from 'df' ?   I.e., how to get
>>>
>>>> identical(nv, .......)
>>>
>>> [1] TRUE
>>>
>>> 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.
>>>
>>> Martin
>>>
>>> ______________________________________________
>>> R-devel at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



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