[R] means by year, month and day

jim holtman jholtman at gmail.com
Mon Jul 18 01:14:02 CEST 2016


Here is an example of using dplyr.  Please provide a reasonable subset of
data.  Your was all for the same date.  Use 'dput' to put in your email.

> x <- read.table(text = "     X.YY MM DD hh WDI R.WSP D.GST   PRES  ATMP
DEWP
+   2015  1  1  0 328   3.6   4.5 1028.0   3.8  -3.5
+   2015  1  1  1 300   2.1   2.7 1027.9   3.7  -4.4
+   2015  1  1  2 264   2.4   2.9 1027.7   3.6  -4.5
+   2015  1  1  3 230   4.1   4.5 1027.4   4.2  -3.8
+   2015  1  1  4 242   8.1   9.2 1026.6   4.4  -3.1
+   2015  1  1  5 262   9.3  10.1 1026.6   4.1  -3.8
+   2015  1  1  6 267   8.6   9.6 1026.3   4.2  -3.8
+   2015  1  1  7 264   9.3   9.9 1026.1   3.9  -2.8
+   2015  1  1  8 268   8.2   9.1 1026.1   3.5  -3.0
+   2015  1  1  9 272   8.8   9.6 1025.4   3.2  -3.3
+   2015  2  1  0 328   3.6   4.5 1028.0   3.8  -3.5
+   2015  2  1  1 300   2.1   2.7 1027.9   3.7  -4.4
+   2015  2  1  2 264   2.4   2.9 1027.7   3.6  -4.5
+   2015  2  1  3 230   4.1   4.5 1027.4   4.2  -3.8
+   2015  2  1  4 242   8.1   9.2 1026.6   4.4  -3.1
+   2015  2  1  5 262   9.3  10.1 1026.6   4.1  -3.8
+   2015  2  1  6 267   8.6   9.6 1026.3   4.2  -3.8
+   2015  2  1  7 264   9.3   9.9 1026.1   3.9  -2.8
+   2015  2  1  8 268   8.2   9.1 1026.1   3.5  -3.0
+   2015  2  1  9 272   8.8   9.6 1025.4   3.2  -3.3
+   2015  3  1  0 328   3.6   4.5 1028.0   3.8  -3.5
+   2015  3  1  1 300   2.1   2.7 1027.9   3.7  -4.4
+   2015  3  1  2 264   2.4   2.9 1027.7   3.6  -4.5
+   2015  3  1  3 230   4.1   4.5 1027.4   4.2  -3.8
+   2015  3  1  4 242   8.1   9.2 1026.6   4.4  -3.1
+   2015  3  1  5 262   9.3  10.1 1026.6   4.1  -3.8
+   2015  3  1  6 267   8.6   9.6 1026.3   4.2  -3.8
+   2015  3  1  7 264   9.3   9.9 1026.1   3.9  -2.8
+   2015  3  1  8 268   8.2   9.1 1026.1   3.5  -3.0
+   2015  3  1  9 272   8.8   9.6 1025.4   3.2  -3.3
+  ",
+  header = TRUE,
+  as.is = TRUE)
>
>  library(dplyr)
>  by_year <- x %>%
+             group_by(X.YY) %>%
+             summarise_each(funs(mean))
>
>  by_ym <- x %>%
+             group_by(X.YY, MM ) %>%
+             summarise_each(funs(mean))
>
>  by_ymd <- x %>%
+             group_by(X.YY, MM, DD) %>%
+             summarise_each(funs(mean))
>
> by_year
Source: local data frame [1 x 10]
   X.YY    MM    DD    hh   WDI R.WSP D.GST    PRES  ATMP  DEWP
  <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl>
1  2015     2     1   4.5 269.7  6.45  7.21 1026.81  3.86  -3.6
> by_ym
Source: local data frame [3 x 10]
Groups: X.YY [?]
   X.YY    MM    DD    hh   WDI R.WSP D.GST    PRES  ATMP  DEWP
  <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl>
1  2015     1     1   4.5 269.7  6.45  7.21 1026.81  3.86  -3.6
2  2015     2     1   4.5 269.7  6.45  7.21 1026.81  3.86  -3.6
3  2015     3     1   4.5 269.7  6.45  7.21 1026.81  3.86  -3.6
> by_ymd
Source: local data frame [3 x 10]
Groups: X.YY, MM [?]
   X.YY    MM    DD    hh   WDI R.WSP D.GST    PRES  ATMP  DEWP
  <int> <int> <int> <dbl> <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl>
1  2015     1     1   4.5 269.7  6.45  7.21 1026.81  3.86  -3.6
2  2015     2     1   4.5 269.7  6.45  7.21 1026.81  3.86  -3.6
3  2015     3     1   4.5 269.7  6.45  7.21 1026.81  3.86  -3.6
>


Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Sun, Jul 17, 2016 at 5:42 PM, Jianling Fan <fanjianling at gmail.com> wrote:

> Hello Tom,
>
> try aggregate() or cast(). Both works.I prefer the latter.
>
>
> library(reshape)
> desc<-melt(mydata, measure.vars=c("WDI","R.WSP", "D.GST", "PRES",
> "ATMP", "DEWP"),
>            id.vars=c("X.YY","MM","DD"))
> summary<-cast(desc, X.YY+MM+DD~variable, mean)
>
>
>
>
>
>
>
>
>
> On 17 July 2016 at 06:22, Tom Mosca <tom at vims.edu> wrote:
> > Hello Good Folk,
> >
> > My dataframe looks like this:
> >> mydata
> >      X.YY MM DD hh WDI R.WSP D.GST   PRES  ATMP  DEWP
> > 1    2015  1  1  0 328   3.6   4.5 1028.0   3.8  -3.5
> > 2    2015  1  1  1 300   2.1   2.7 1027.9   3.7  -4.4
> > 3    2015  1  1  2 264   2.4   2.9 1027.7   3.6  -4.5
> > 4    2015  1  1  3 230   4.1   4.5 1027.4   4.2  -3.8
> > 5    2015  1  1  4 242   8.1   9.2 1026.6   4.4  -3.1
> > 6    2015  1  1  5 262   9.3  10.1 1026.6   4.1  -3.8
> > 7    2015  1  1  6 267   8.6   9.6 1026.3   4.2  -3.8
> > 8    2015  1  1  7 264   9.3   9.9 1026.1   3.9  -2.8
> > 9    2015  1  1  8 268   8.2   9.1 1026.1   3.5  -3.0
> > 10   2015  1  1  9 272   8.8   9.6 1025.4   3.2  -3.3 …
> >
> > The first four columns are year, month, day, hour (0 – 23).  I wish to
> take the means of the next six columns (WDIR, WSPD, GST, PRES, ATMP and
> DEWP) by year, month and day.  That is, I want daily averages.
> >
> > Please help.  Thank you.
> >
> > Tom
> >
> >         [[alternative HTML version deleted]]
> >
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
>
>
> --
> Jianling Fan
> 樊建凌
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list