简体   繁体   中英

convert multivariate weekly data in to monthly data in R

I am trying to convert weekly multivariate data into monthly data, however it does not produce the correct results. I was wondering if someone could please help me to achieve the same. Below is how the dataframe looks like

Week          Display             Native     Video          TV
2017-03-12    2951.86            1158.6     2626.05        1416.28
2017-03-19    2951.86            1158.6     2626.05        1416.28
2017-03-26    2614.12            1061.99    2283.19        1505.28
2017-04-02    587.67             482.33     226.05         2039.3
2017-04-09    587.67             482.33     226.05         2039.3
2017-04-16    587.67             482.33     226.05         2039.3
2017-04-23    587.67             482.33     226.05         2039.3
2017-04-30    516.31             1477.81    244.98         5994.72
2017-05-07    504.42             1643.72    248.14         6653.95
2017-05-14    504.42             1643.72    248.14         6653.95
2017-05-21    504.42             1643.72    248.14         6653.95
2017-05-28    735.05             2325.75    241.46         3983.36
2017-06-04    1042.56            3235.12    232.56         422.56
2017-06-11    1042.56            3235.12    232.56         422.56
2017-06-18    1042.56            3235.12    232.56         422.56
2017-06-25    893.62             2772.96    228.57         362.19

This is what I have tried but does not produce the correct results

to.day <- function(i) with(data, zoo(Display...Other[i], seq(Weeksart[i], weekend[i], "day")))
z.day <- do.call(c, lapply(1:nrow(data), to.day))
aggregate(z.day, as.yearmon, sum)

Any help would be highly appreciated. Thanks in advance!!

exdata <- read.table(text = "
  Week          Display            Native     Video          TV
  2017-03-12    2951.86            1158.6     2626.05        1416.28
  2017-03-19    2951.86            1158.6     2626.05        1416.28
  2017-03-26    2614.12            1061.99    2283.19        1505.28
  2017-04-02    587.67             482.33     226.05         2039.3
  2017-04-09    587.67             482.33     226.05         2039.3
  2017-04-16    587.67             482.33     226.05         2039.3
  2017-04-23    587.67             482.33     226.05         2039.3
  2017-04-30    516.31             1477.81    244.98         5994.72
  2017-05-07    504.42             1643.72    248.14         6653.95
  2017-05-14    504.42             1643.72    248.14         6653.95
  2017-05-21    504.42             1643.72    248.14         6653.95
  2017-05-28    735.05             2325.75    241.46         3983.36
  2017-06-04    1042.56            3235.12    232.56         422.56
  2017-06-11    1042.56            3235.12    232.56         422.56
  2017-06-18    1042.56            3235.12    232.56         422.56
  2017-06-25    893.62             2772.96    228.57         362.19",
  header = TRUE)

exdata$Week <- format(as.POSIXct(exdata$Week), "%b/%Y") # or any other format you want

> aggregate(. ~ Week, exdata, sum)
      Week Display   Native   Video       TV
1 abr/2017 2866.99  3407.13 1149.18 14151.92
2 jun/2017 4021.30 12478.32  926.25  1629.87
3 mai/2017 2248.31  7256.91  985.88 23945.21
4 mar/2017 8517.84  3379.19 7535.29  4337.84

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM