From maechler at stat.math.ethz.ch  Sat Aug 18 18:20:51 2012
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Sat, 18 Aug 2012 18:20:51 +0200
Subject: [Rd] Quiz: How to get a "named column" from a data frame
In-Reply-To: <CAJLQA-6huzGgtWhb4h9hqm3ZBctQW97EexYMNXK-+6UsAzERdw@mail.gmail.com>
References: <20527.44736.486888.854733@stat.math.ethz.ch>
	<CAJLQA-6huzGgtWhb4h9hqm3ZBctQW97EexYMNXK-+6UsAzERdw@mail.gmail.com>
Message-ID: <CAPRP4-cvBERVf1SOVE1ODy7Hh-GbQWbQjgF8636zotLLreeuNA@mail.gmail.com>

On Sat, Aug 18, 2012 at 5:14 PM, Christian Brechb?hler .... wrote:
> On Sat, Aug 18, 2012 at 11: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)?
>
>
>> identical(nv, df[,1])
> [1] TRUE
>
>> In my solution, the above '.......' consists of 17 letters.
>
>
> I count 6 in mine

But it is not a solution in a current version of R!
though it's still interesting that   df[,1]  worked in some incantation of R.

What's your sessionInfo()?
Martin

>
> /Christian



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