Aggregate ISO weeks into months with a dataset containing just ISO weeks

前端 未结 4 987
孤城傲影
孤城傲影 2021-01-14 03:25

My data is in a dataframe which has a structure like this:

df2 <- data.frame(Year = c(\"2007\"), Week = c(1:12), Measurement = c(rnorm(12, mean = 4, sd =          


        
4条回答
  •  暖寄归人
    2021-01-14 04:07

    This can be done relatively simply in dplyr.

    library(dplyr)
    
    df2 %>% 
      mutate(Month = rep(1:3, each = 4)) %>% 
      group_by(Month) %>% 
      summarise(MonthlyMedian = stats::median(Measurement))
    

    Basically, add a new column to define your months. I'm presuming since you don't have days, you are going to allocate 4 weeks per month? Then you just group by your Month variable and calculate the median. Very simple

    Hope this helps

提交回复
热议问题