[R] getting numeric arrays from data frame
jfox at mcmaster.ca
Mon Apr 7 13:28:08 CEST 2008
A data frame is a list, and thus foo returns a one-element list, while
foo[] returns a vector. One can also subscript a data frame as a matrix,
so foo[1, 5] returns the element in the 1st row, 5th column, foo[,5] returns
the 5th column as a vector, and foo[,c(3, 5)] returns a two-column data
frame (and is equivalent to foo[c(3, 5)]), while foo[[c(3, 5)]] returns the
element in column 3, row 5 (i.e., the 5th element of the 3rd list element).
I hope this helps,
John Fox, Professor
Department of Sociology
Hamilton, Ontario, Canada L8S 4M4
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Mark Farnell
> Sent: April-06-08 11:15 PM
> To: r-help at r-project.org
> Subject: [R] getting numeric arrays from data frame
> Currently I invoke:
> foo <- read.table("foo.data", header=TRUE)
> to read a table into foo
> Then when I try to plot a histogram out of the 5th column of foo:
> It fails and it says:
> Error in hist.default(foo) : 'x' must be numeric
> Then I tried:
>  "List"
> So how can I get an numeric array out of one of the columns?
> Also suppose if the dataframe foo (above) has eight columns and I want
> to make an x-y scatter plot on two of the columns, how can I extract
> these two specific columns?
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help