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. 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.
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)> > 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
So how would I convert those row names to dates and give that column the name "Date" so that I can use subset and other functions on the Date column ? -- View this message in context: http://r.789695.n4.nabble.com/Accessing-DF-index-tp3314649p3314689.html Sent from the R help mailing list archive at Nabble.com.
On Feb 19, 2011, at 5:25 PM, eric wrote:> > So how would I convert those row names to dates and give that column > the name > "Date" so that I can use subset and other functions on the Date > column ?> x2$dtcol <- as.Date(rownames(x2)) > x2 FAIRX SP500 delta dtcol 2000-08-31 0.010101096 0.007426964 0.002674132 2000-08-31 2000-09-29 0.096679730 -0.054966292 0.151646023 2000-09-29 2000-10-31 -0.008245580 -0.004961785 -0.003283795 2000-10-31 2000-11-30 0.037024545 -0.083456134 0.120480679 2000-11-30 2000-12-29 0.080042708 0.004045193 0.075997514 2000-12-29 2001-01-31 -0.009042396 0.034050246 -0.043092643 2001-01-31> -- > View this message in context: http://r.789695.n4.nabble.com/Accessing-DF-index-tp3314649p3314689.html > Sent from the R help mailing list archive at Nabble.com.David Winsemius, MD West Hartford, CT
On Feb 19, 2011, at 5:25 PM, eric wrote:> > So how would I convert those row names to dates and give that column > the name > "Date" so that I can use subset and other functions on the Date > column ?You could have used subset without the new column: > subset(x2, rownames(x2) < "2000-11-30") FAIRX SP500 delta dtcol 2000-08-31 0.01010110 0.007426964 0.002674132 2000-08-31 2000-09-29 0.09667973 -0.054966292 0.151646023 2000-09-29 2000-10-31 -0.00824558 -0.004961785 -0.003283795 2000-10-31> --David Winsemius, MD West Hartford, CT