Home > Blockchain >  Get mean of a column (group by month) - Dplyr
Get mean of a column (group by month) - Dplyr

Time:10-28

I have a simple dataframe like this:

dput(a)
structure(list(date = structure(c(1640995200, 1641081600, 1641168000, 
1641254400, 1641340800, 1641427200, 1643673600, 1643760000, 1643846400, 
1643932800, 1644019200, 1644105600, 1646092800, 1646179200, 1646265600, 
1646352000, 1646438400, 1646524800, 1646611200, 1646697600), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), value = c(18, 27, 28, 50, 42, 18, 
18, 39, 16, 49, 42, 26, 18, 38, 15, 50, 18, 10, 36, 22)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -20L))

The value column is for daily dates. How do I get the monthly mean of the column value?

CodePudding user response:

We use the format to modify the dates to year, month and the day as 1 and get the mean

library(dplyr)
a %>%
   group_by(month = format(date, '%Y/%m/01')) %>%
  summarise(value = mean(value, na.rm = TRUE))

-output

# A tibble: 3 × 2
  month      value
  <chr>      <dbl>
1 2022/01/01  30.5
2 2022/02/01  31.7
3 2022/03/01  25.9

If it is year month, change the format to format(date, '%Y-%b')

  • Related