Skip to content

Extracting a column of numbers from a data frame

2 messages · Jason Horn, Douglas Bates

#
When using RMySQL to retrieve data from a database, the result is  
always a data frame.  What is the best way to take get the numbers  
from a column of the data frame into a vector?  That is, I just want  
the column of numbers from the data frame and not the column label.   
I'm sure this has been done many times, but I'm new to this, so I  
thought I'd ask.

Thanks,

- Jason
#
On 2/20/06, Jason Horn <jhorn at bu.edu> wrote:
You use the "[[" subscripting operator or its short form for use with
names, the $ operator.

If your data frame is called "df" and you want the third column as a vector use

df[[3]]

If the name of the column is "foo" you can also use

df[["foo"]]

The expression

df$foo

is syntactic sugar for df[["foo"]]