[R] extracting named vector from dataframe

baptiste auguie baptiste.auguie at googlemail.com
Sun Oct 31 17:11:55 CET 2010


Hi,

I think you want ?unlist

d = data.frame(x=1, y=2, z=3)
v = unlist(d)
is(v)
[1] "numeric" "vector"

HTH,

baptiste

On 31 October 2010 16:54, James Hirschorn <James.Hirschorn at hotmail.com> wrote:
> Suppose df is a dataframe with one named row of numeric observations. I want
> to coerce df into a named vector.
>
>
>
> as.vector does not work as I expected: as.vector(df) returns the original
> dataframe, while as.vector(df,mode="numeric") returns an unnamed vector of
> NAs.
>
>
>
> This works:
>
>
>
>> v <- as.numeric(as.matrix(df)); names(v) <- names(df);
>
>
>
> I just wanted check if there was a better/more natural way of doing this?
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list