[R] After sorting a dataframe by date

MacQueen, Don macqueen1 at llnl.gov
Thu Oct 25 16:46:36 CEST 2012


As David said, you sorted by Date.

But sorting by rownames is not really the point. The point is that
rownames are not line numbers.

The rownames were assigned when the data frame was created, and then
preserved when you sorted.  Sometimes, rownames contain meaningful
information that is associated with the data in that row, and therefore
must be sorted along with the rows.

If you want to change the row names to be analogous to line numbers after
sorting you can do, for example,

   rownames(sorted.df) <- seq(nrow(sorted.df))




-Don

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 10/23/12 9:34 AM, "martiny" <my2108 at outlook.com> wrote:

>HI,
>I have created a dataframe "df" and try to sort it by its date using the
>order() as below:
>
>df<-read.csv(constr,header=T)
>  sorted.df<-df[order(as.Date(df$Date), decreasing = F),]
>  print(sorted.df)
>
>The dataframe was sorted, but the output from the command console shows
>reserved line order..rather than starting at "1"....it does not really
>affect my result, but I want to understand why is that...
>
>          Date   Open   High    Low  Close   Volume Adj.Close
>252 2011-01-03 325.64 330.26 324.84 329.57 15897800    328.16
>251 2011-01-04 332.44 332.50 328.15 331.29 11038600    329.87
>250 2011-01-05 329.55 334.34 329.50 334.00  9125700    332.57
>249 2011-01-06 334.72 335.25 332.90 333.73 10729600    332.30
>248 2011-01-07 333.99 336.35 331.90 336.12 11140400    334.68
>247 2011-01-10 338.83 343.23 337.17 342.45 16020000    340.99
>246 2011-01-11 344.88 344.96 339.47 341.64 15861000    340.18
>245 2011-01-12 343.25 344.43 342.00 344.42 10806800    342.95
>244 2011-01-13 345.16 346.64 343.85 345.68 10599300    344.20
>...
>...
>3   2011-12-28 406.89 408.25 401.34 402.64  8166500    400.92
>2   2011-12-29 403.40 405.65 400.51 405.12  7713500    403.39
>1   2011-12-30 403.51 406.28 403.49 405.00  6416500    403.27
>
>Any advice would be highly appreciated.
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/After-sorting-a-dataframe-by-date-tp4647173.
>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.




More information about the R-help mailing list