[R] After sorting a dataframe by date

David Winsemius dwinsemius at comcast.net
Tue Oct 23 22:02:23 CEST 2012


On Oct 23, 2012, at 9:34 AM, martiny 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

It is the dates that were sorted in a non-decreasing order, not the rownames.

-- 

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list