[R-sig-Geo] Subsetting dataframe by all factor levels

Gabriel Gaona g@vg712 @ending from gm@il@com
Mon Sep 17 02:15:48 CEST 2018


An other option is the tydiverse way. I assume when you say " My goal is to
use the monthly mean rainfall at each of the 58 reporting stations..." you
mean you want for each year and station a average value of monthly prcp .
From your input data frame, is like:

library(dplyr)
library(lubridate)
rainfall_yearly <- rainfall %>%
    group_by(name,
        Month = floor_date(sampdate, "month")) %>% #Monthly round down
dates for grouping
    summarise(prcp = sum(prcp, na.rm = TRUE) %>% #calculating monthly prcp
    group_by(name,
        Year = floor_date(Month, "year")) %>% #Yearly round down dates for
grouping
    summarise(prcp = mean(prcp, na.rm = TRUE) #calculating monthly average
per year
_________________________
* Gabriel Gaona*
*Teléfono*: +593 9 91665888
*Twitter/Hangouts*: gavg712
Loja - Ecuador
https://www.researchgate.net/profile/Gabriel_Gaona


El vie., 14 sept. 2018 a las 17:54, Rich Shepard (<rshepard using appl-ecosys.com>)
escribió:

> On Fri, 14 Sep 2018, Justin H. wrote:
>
> > I'm not sure how it handles your date format. It's probably grouping
> > things weirdly. If you can split out that column into two columns, one
> for
> > year and one for month. I'd have to tinker with it later as I can't think
> > of code off the top of my head. It should play nicer then.
>
> Justin,
>
>    The rainfall data.frame structure:
> 'data.frame':   113569 obs. of  6 variables:
>   $ name    : Factor w/ 58 levels "Blazed Alder",..: 20 20 20 20 20 20 20
> 20 ...
>   $ easting : num  2370575 2370575 2370575 2370575 2370575 ...
>   $ northing: num  199338 199338 199338 199338 199338 ...
>   $ elev    : num  228 228 228 228 228 228 228 228 228 228 ...
>   $ sampdate: Date, format: "2005-01-01" "2005-01-02" ...
>   $ prcp    : num  0.59 0.08 0.1 0 0 0.02 0.05 0.1 0 0.02 ...
>
> After splitting by name (only the first one shown):
> str(rainfall_by_site)
> List of 58
>   $ Blazed Alder                 :'data.frame':      4900 obs. of  6
> variables:
>    ..$ name    : Factor w/ 58 levels "Blazed Alder",..: 1 1 1 1 1 1 1 1 1
> 1 ...
>    ..$ easting : num [1:4900] 2393589 2393589 2393589 2393589 2393589 ...
>    ..$ northing: num [1:4900] 196841 196841 196841 196841 196841 ...
>    ..$ elev    : num [1:4900] 1112 1112 1112 1112 1112 ...
>    ..$ sampdate: Date[1:4900], format: "2005-01-01" "2005-01-02" ...
>    ..$ prcp    : num [1:4900] 0.2 0.2 0.4 0 0 0 0.1 0.1 0.1 0.2 ...
>
> Adding a year column to the end:
>      $ year    : num  0 0 0 0 0 0 0 0 0 0 ...
>
> I've not separated the sampdate structure into years and months; I can and
> that might make the difference. Will try to find time this weekend to do
> so.
> Otherwise, it'll be next week.
>
> Regards,
>
> Rich
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list