user5619709 user5619709 - 10 months ago 41
R Question

Obtaining 0th column data frame information in R

How would you access the date column on the left here of this data frame?

> data
2005-02-28 0.48326866256
2005-04-30 0.64922340405
2005-05-31 0.70036043096
2005-06-30 0.72051442515

If I type,

data[ , 1]

… it gives me the rate column. As expected.

If I use,

data[ , 0]

... it doesn’t work. And I get 'Error: subscript out of bounds'.

But if I do,

data[1 , 0]

... I get back the first date. 2005-02-28.

This seems a bit contradictory to me that I can't pull up data[ , 0], given that I can pull up data[ , 1] and can also pull up data[1 , 0].

How do I pull up that those date values? And why doesn't data[ , 0] work?


Answer Source

There’s no 0th column. What you are probably seeing, rather, are row names. You can access them via:

rownames(data) # for a matrix-like object
row.names(data) # for a data.frame

But either will work just fine.