Re: [R] getting numeric arrays from data frame

From: John Fox <jfox_at_mcmaster.ca>
Date: Mon, 07 Apr 2008 07:28:08 -0400

Dear Mark,

A data frame is a list, and thus foo[5] returns a one-element list, while foo[[5]] 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



John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox

> -----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:
>
> hist(foo[5])
>
> It fails and it says:
>
> Error in hist.default(foo[5]) : 'x' must be numeric
>
> Then I tried:
>
> >typeof(foo[5])
> [1] "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?
>
> Thanks
>
> Mark
>
> ______________________________________________
> 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.



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. Received on Mon 07 Apr 2008 - 11:33:01 GMT

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 Mon 07 Apr 2008 - 12:30:26 GMT.

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

list of date sections of archive