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

前端 未结 4 985
孤城傲影
孤城傲影 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:14

    using dplyr you can try:

    require(dplyr)
    
    df2 %>% mutate(Date = as.Date(paste("1", Week, Year, sep = "-"), format = "%w-%W-%Y"),
                Year_Mon = format(Date,"%Y-%m")) %>% group_by(Year_Mon) %>%
                summarise(result = median(Measurement))
    

    As @djhrio pointed out, Thursday is used to determine the weeks in a month. So simply switch paste("1", to paste("4", in the code above.

提交回复
热议问题