From brechbuehler at gmail.com  Sat Aug 18 20:13:20 2012
From: brechbuehler at gmail.com (=?ISO-8859-1?Q?Christian_Brechb=FChler?=)
Date: Sat, 18 Aug 2012 14:13:20 -0400
Subject: [Rd] Quiz: How to get a "named column" from a data frame
In-Reply-To: <CAPRP4-cvBERVf1SOVE1ODy7Hh-GbQWbQjgF8636zotLLreeuNA@mail.gmail.com>
References: <20527.44736.486888.854733@stat.math.ethz.ch>
	<CAJLQA-6huzGgtWhb4h9hqm3ZBctQW97EexYMNXK-+6UsAzERdw@mail.gmail.com>
	<CAPRP4-cvBERVf1SOVE1ODy7Hh-GbQWbQjgF8636zotLLreeuNA@mail.gmail.com>
Message-ID: <CAJLQA-4qk6WMm1y7dksHFTD_yTiajFPaTXa7WaWxsf1w-PjJ9g@mail.gmail.com>

On 8/18/12, Martin Maechler <maechler at 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
>> <maechler at stat.math.ethz.ch> wrote:

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

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

My mistake!  We disliked some quirks of indexing, so we've long had
our own patch for "[.data.frame" in place, which I used inadvertently.
 In essence, it does this:

    result <- base::"[.data.frame"(df,,1, drop=F)
    if (drop && length(ncol(result) > 0) && ncol(result)==1) {
          save.names <- dimnames(result)[[1]]
          result <- result[[1]]
          names(result) <- save.names
    }

That obviously violated your constraint "no non-standard R packages".
I apologize.

Still, maybe the behavior of getting the named column would be
desirable in general?

/Christian



[Rd] Quiz: How to get a "named column" from a data frame
=?ISO-8859-1?Q?Christian_Brechb=FChler?=