Home > other >  How to find average of variable (x value) aggregated by subset (certain days) of another variable (y
How to find average of variable (x value) aggregated by subset (certain days) of another variable (y

Time:04-19

I would like to see if it is possible to use the aggregate function to find the mean for values from Sep. to Oct. of multiple years. I would like to compare the average rainfall in the 2021-9-1 to 2021-10-31 range to the same range of previous years without having to write out a mean calculation for each one.

DANHP has 12 variables, of which I am using:

  • value (rainfall in inches)
  • DATE (YYYY-MM-DD in date format)
  • JULIAN (the Julian day 1-365)
  • Month (1-12, extracted from DATE)
  • Year (2012-2021, extracted from DATE) Here is the code I am working with:
aggregate(DANHP$value, list(DANHP$Year), subset(filter(DANHP$JULIAN >= 244 & DANHP$JULIAN <= 304)),
          FUN=mean)
Error in UseMethod("filter") : 
  no applicable method for 'filter' applied to an object of class "logical"

It doesn't look like I can define a subset for the command by filtering, so I would like to know if there is another method.

Ideally, the output would be similar to the below output, but this output calculates the means for all days in each year. I would like to see the means for the same subset of each year: Sep. 1 - Oct. 31 (Julian days 244-304).

aggregate(DANHP$value, list(DANHP$Year), FUN=mean)
   Group.1         x
1     2012 0.1455956
2     2013 0.1586712
3     2014 0.1376849
4     2015 0.1338904
5     2016 0.1476230
6     2017 0.2518082
7     2018 0.1467507
8     2019 0.1400603
9     2020 0.1759290
10    2021 0.1520301
11    2022 0.0000000

Thanks in advance!

CodePudding user response:

There is a syntax error in the subset code, where a dplyr::filter (may be used). Instead, the subset itself can use subset argument inside

aggregate(value ~ Year, subset(DANHP, JULIAN >= 244 & JULIAN <=304), mean)
  • Related