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

From: Bert Gunter <gunter.berton_at_gene.com>
Date: Sat, 18 Aug 2012 09:14:41 -0700

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
h 8 H
i 9 I
j 10 J
> y <- t(df[,1,drop=FALSE])[1,]
> y

 a b c d e f g h i j
 1 2 3 4 5 6 7 8 9 10
> identical(x,y)

[1] TRUE Cheers,
Bert

On Sat, Aug 18, 2012 at 8:16 AM, Joshua Ulrich <josh.m.ulrich_at_gmail.com> wrote:
> 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

-- 

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

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Sun 19 Aug 2012 - 13:32:12 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 19 Aug 2012 - 13:40:39 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive