[R] monthly median in a daily dataset

Bill.Venables at csiro.au Bill.Venables at csiro.au
Mon Dec 20 03:17:59 CET 2010


I find this function useful for digging out months from Date objects

Month <- function(date, ...)
  factor(month.abb[as.POSIXlt(date)$mon + 1], levels = month.abb) 

For this little data set below this is what it gives

> with(data, tapply(value, Month(date), median, na.rm = TRUE))
  Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec 
   NA    NA    NA    NA    NA    NA    NA    NA    NA    NA 0.035 0.030

Here is another useful little one:

Year <- function(date, ...)
  as.POSIXlt(date)$year + 1900

So if you wanted the median by year and month you could do

> with(data, tapply(value, list(Year(date), Month(date)), median, na.rm = TRUE))
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct   Nov  Dec
2008  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA 0.035 0.03

(The result is a matrix, which in this case has only one row, of course.)

See how you go.

Bill Venables.

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of HUXTERE
Sent: Monday, 20 December 2010 8:32 AM
To: r-help at r-project.org
Subject: [R] monthly median in a daily dataset


Hello,

I have a multi-year dataset (see below) with date, a data value and a flag
for the data value. I want to find the monthly median for each month in this
dataset and then plot it. If anyone has suggestions they would be greatly
apperciated. It should be noted that there are some dates with no values and
they should be removed.

Thanks
Emily

> print ( str(data$flow$daily) )
'data.frame':   16071 obs. of  3 variables:
 $ date :Class 'Date'  num [1:16071] -1826 -1825 -1824 -1823 -1822 ...
 $ value: num  NA NA NA NA NA NA NA NA NA NA ...
 $ flag : chr  "" "" "" "" ...
NULL

520    2008-11-01 0.034     
1041   2008-11-02 0.034     
1562   2008-11-03 0.034     
2083   2008-11-04 0.038     
2604   2008-11-05 0.036     
3125   2008-11-06 0.035     
3646   2008-11-07 0.036     
4167   2008-11-08 0.039     
4688   2008-11-09 0.039     
5209   2008-11-10 0.039     
5730   2008-11-11 0.038     
6251   2008-11-12 0.039     
6772   2008-11-13 0.039     
7293   2008-11-14 0.038     
7814   2008-11-15 0.037     
8335   2008-11-16 0.037     
8855   2008-11-17 0.037     
9375   2008-11-18 0.037     
9895   2008-11-19 0.034    B
10415  2008-11-20 0.034    B
10935  2008-11-21 0.033    B
11455  2008-11-22 0.034    B
11975  2008-11-23 0.034    B
12495  2008-11-24 0.034    B
13016  2008-11-25 0.034    B
13537  2008-11-26 0.033    B
14058  2008-11-27 0.033    B
14579  2008-11-28 0.033    B
15068  2008-11-29 0.034    B
15546  2008-11-30 0.035    B
521    2008-12-01 0.035    B
1042   2008-12-02 0.034    B
1563   2008-12-03 0.033    B
2084   2008-12-04 0.031    B
2605   2008-12-05 0.031    B
3126   2008-12-06 0.031    B
3647   2008-12-07 0.032    B
4168   2008-12-08 0.032    B
4689   2008-12-09 0.032    B
5210   2008-12-10 0.033    B
5731   2008-12-11 0.033    B
6252   2008-12-12 0.032    B
6773   2008-12-13 0.031    B
7294   2008-12-14 0.030    B
7815   2008-12-15 0.030    B
8336   2008-12-16 0.029    B
8856   2008-12-17 0.028    B
9376   2008-12-18 0.028    B
9896   2008-12-19 0.028    B
10416  2008-12-20 0.027    B
10936  2008-12-21 0.027    B
11456  2008-12-22 0.028    B
11976  2008-12-23 0.028    B
12496  2008-12-24 0.029    B
13017  2008-12-25 0.029    B
13538  2008-12-26 0.029    B
14059  2008-12-27 0.030    B
14580  2008-12-28 0.030    B
15069  2008-12-29 0.030    B
15547  2008-12-30 0.031    B
15851  2008-12-31 0.031    B
-- 
View this message in context: http://r.789695.n4.nabble.com/monthly-median-in-a-daily-dataset-tp3094917p3094917.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