[R] extracting named vector from dataframe
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@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.
Re: [R] extracting named vector from dataframe
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.hirsch...@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@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. __ R-help@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.
Re: [R] extracting named vector from dataframe
On Oct 31, 2010, at 11:54 AM, James Hirschorn wrote: Suppose df is a dataframe with one named row of numeric observations. I want to coerce df into a named vector. I don't think you understand the structure of dataframes. They are named lists of component columns. The names you are attributing to the rows are not attached to the observations but rather are column names. So that row is not in any sense a named vector. If you created a dataframe with the first column a named vector its names would become the rownames. 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); Right. You are now assigning the column names to the elements in a row, but in some ways that is an unnatural act, and not something that would be expected to work in the general case where a row might be a diverse set of types and even different classes. Your as.matrix operation coerced all of the values to be of one type. I just wanted check if there was a better/more natural way of doing this? -- David Winsemius, MD West Hartford, CT __ R-help@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.
Re: [R] extracting named vector from dataframe
Of course you are right that this would not be appropriate in general, but what I'm doing--which as Baptiste explained can be done much more nicely with unlist()---seems reasonable in my context: The dataframe has a computed statistic for each input, but I need a vector so that I can do operations such as sort(). -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Sunday, October 31, 2010 12:24 PM To: James Hirschorn Cc: R-help@r-project.org Subject: Re: [R] extracting named vector from dataframe On Oct 31, 2010, at 11:54 AM, James Hirschorn wrote: Suppose df is a dataframe with one named row of numeric observations. I want to coerce df into a named vector. I don't think you understand the structure of dataframes. They are named lists of component columns. The names you are attributing to the rows are not attached to the observations but rather are column names. So that row is not in any sense a named vector. If you created a dataframe with the first column a named vector its names would become the rownames. 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); Right. You are now assigning the column names to the elements in a row, but in some ways that is an unnatural act, and not something that would be expected to work in the general case where a row might be a diverse set of types and even different classes. Your as.matrix operation coerced all of the values to be of one type. I just wanted check if there was a better/more natural way of doing this? -- David Winsemius, MD West Hartford, CT __ R-help@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.
Re: [R] extracting named vector from dataframe
On Sun, Oct 31, 2010 at 12:11 PM, baptiste auguie baptiste.aug...@googlemail.com wrote: Hi, I think you want ?unlist d = data.frame(x=1, y=2, z=3) v = unlist(d) is(v) [1] numeric vector Here are a few other possibilities too: drop(as.matrix(d)) do.call(c, d) sapply(d, identity) -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ R-help@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.