[R] Accessing DF index

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Feb 20 08:47:32 CET 2011


On Sat, 19 Feb 2011, David Winsemius wrote:

>
> On Feb 19, 2011, at 4:50 PM, eric wrote:
>
>> 
>> I have a dataframe called x2. It seems to have a date column but I can't
>> access it or give it a name or convert it to a date. How do I refer to that
>> first column and make it a date ? When I try x2[1,] I get the second 
>> column.
>
> It is not actually a column but is rather a set of row names:
>
> ?rownames
>
> rownames(x2)

More precisely, ?row.names for a DF: rownames is for a matrix.  As 
the help you quoted says

      For a data frame, ‘rownames’
      and ‘colnames’ eventually call ‘row.names’ and ‘names’
      respectively, but the latter are preferred.


>
>
>> 
>> head(x2)
>>                 FAIRX        SP500        delta
>> 2000-08-31  0.010101096  0.007426964  0.002674132
>> 2000-09-29  0.096679730 -0.054966292  0.151646023
>> 2000-10-31 -0.008245580 -0.004961785 -0.003283795
>> 2000-11-30  0.037024545 -0.083456134  0.120480679
>> 2000-12-29  0.080042708  0.004045193  0.075997514
>> 2001-01-31 -0.009042396  0.034050246 -0.043092643
>> 
>> x[1,1]
>>               FAIRX
>> 2000-08-31 0.01010110
>> 
>> x[1,2]
>>                SP500
>> 2000-08-31 0.007426964
>> 
>> str(x2)
>> 'data.frame':	127 obs. of  3 variables:
>> $ FAIRX: num  0.0101 0.09668 -0.00825 0.03702 0.08004 ...
>> $ SP500: num  0.00743 -0.05497 -0.00496 -0.08346 0.00405 ...
>> $ delta: num  0.00267 0.15165 -0.00328 0.12048 0.076 ..
>> -- 
>> View this message in context: 
>> http://r.789695.n4.nabble.com/Accessing-DF-index-tp3314649p3314649.html
>> Sent from the R help mailing list archive at Nabble.com.
>> 
>> ______________________________________________
>> 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.
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list