Aggregated weekly

I have a daily data frame, and my task is to aggregate it to be weekly averages but I dont know what to do and use what function? Pls help

| | Date| Price | Week num |

Using tidyverse, you can use mutate() with group_by(`Week num`) or the more modern .by.

An alternative to tidyverse is {data.table}

DT <- data.table::data.table(
              Date = c("2014-01-01","2014-01-02",
             Price = c(NA,562,562.8,566.3,568.15,
    Week.num = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L)

DT1 <- na.omit(DT) # drop rows that have an NA. 

DT3 <- DT1[, .(weekly_avg = mean(Price)), key = Week.num]


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.