[R] arrange data

Bill.Venables at csiro.au Bill.Venables at csiro.au
Tue Jun 1 07:48:22 CEST 2010


Here is one way.  Suppose your data frame is called 'dat'.

o <- with(dat, order(as.Date(paste(year, month, day, sep="-"))))
newDat <- dat[o, c("year", "month", "day", "rain")]

 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Roslina Zakaria
Sent: Tuesday, 1 June 2010 2:55 PM
To: r-help at r-project.org
Cc: roslinazairimah zakaria
Subject: [R] arrange data

Hi r-users,
 
I have a data below but I would like to rearrange my data so that it will be in the following format: year, month, day, rain.  I also would like the data to be arranged so that it will be continues for example year 1939 it will go from january 1 to december 31 and continue with year 1940 from january 1st to 31st December and so on.  What I have now is all january for all the years (1939-1988) and it continue february 1939 to all february 1988 and so on.
 
      day month year rain
1675   1     1 1939  0.0
1676   2     1 1939  0.0
1677   3     1 1939  0.0
1678   4     1 1939  5.8
1679   5     1 1939  0.0
...
41294  29    12 1987  0.0
41295  30    12 1987  0.0
41296  31    12 1987  0.0
41297   1    12 1988  0.0
41298   2    12 1988  0.0
41299   3    12 1988  0.0
41300   4    12 1988  0.7
41301   5    12 1988  6.1
41302   6    12 1988  0.0
41303   7    12 1988  0.0
41304   8    12 1988  0.0
41305   9    12 1988  0.0
41306  10    12 1988  0.0
41307  11    12 1988  0.0
41308  12    12 1988  0.0
41309  13    12 1988  0.0
41310  14    12 1988  0.0
41311  15    12 1988  0.0
41312  16    12 1988  0.0
41313  17    12 1988  0.0
41314  18    12 1988  0.0
41315  19    12 1988  0.0
41316  20    12 1988  1.2
41317  21    12 1988 20.2
41318  22    12 1988  0.4
41319  23    12 1988  0.0
41320  24    12 1988  0.0
41321  25    12 1988  6.5
41322  26    12 1988  7.1
41323  27    12 1988  0.0
41324  28    12 1988  0.0
41325  29    12 1988  0.0
41326  30    12 1988  0.0
41327  31    12 1988  0.0
 
I tried to use aggregate function and I can separate the month but what I really want is the daily format.
 
> pooraka.dt
      [,1] [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10] [,11] [,12]
1885   4.6 21.4  11.6  24.7  49.9  91.3  55.9  64.5  56.1  16.6   7.5  11.5
1886  27.2  7.9   0.5  29.2  30.9   8.8  65.4  60.5  37.0  68.7  21.1  20.3
1887  12.9  4.1   6.7  43.2  67.7 144.8  51.6  42.8  67.3  66.7  29.0  36.1
...
1996  73.2 22.2   9.2  25.4  15.6  69.2  93.0  75.6  67.8  31.2   2.8  18.6
1997  31.2 39.0   9.1   2.4  49.4  18.4  14.0  75.4  61.6  79.2  34.6  28.4
1998  12.0 21.0   6.6  88.6  21.2  67.6  56.6  41.4  36.2  35.4  28.2   5.6

 
Thank you so much for your help.

 


      
	[[alternative HTML version deleted]]



More information about the R-help mailing list