这是我的数据框。
structure(list(date = structure(c(18993, 18994, 18995, 18996,
18997, 18998, 18999, 19000, 19001, 19002, 19003, 19004, 19005,
19006), class = "Date"), sales = c(10, 40, 30, 20, 50, 20, 10,
20, 10, 30, 60, 10, 10, 50)), class = c("tbl_ts", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -14L), key = structure(list(
.rows = structure(list(1:14), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), index = structure("date", ordered = TRUE), index2 = "date", interval = structure(list(
year = 0, quarter = 0, month = 0, week = 0, day = 1, hour = 0,
minute = 0, second = 0, millisecond = 0, microsecond = 0,
nanosecond = 0, unit = 0), .regular = TRUE, class = c("interval",
"vctrs_rcrd", "vctrs_vctr")))
我正在寻找这个输出:
structure(list(date = structure(c(18993, 18994, 18995, 18996,
18997, 18998, 18999, 19000, 19001, 19002, 19003, 19004, 19005,
19006), class = "Date"), sales = c(10, 40, 30, 20, 50, 20, 10,
20, 10, 30, 60, 10, 10, 50), average_total_sales_at_date = c(10,
25, 26.66667, 25, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA,
-14L), key = structure(list(.rows = structure(list(1:14), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L)), index = structure("date", ordered = TRUE), index2 = "date", interval = structure(list(
year = 0, quarter = 0, month = 0, week = 0, day = 1, hour = 0,
minute = 0, second = 0, millisecond = 0, microsecond = 0,
nanosecond = 0, unit = 0), .regular = TRUE, class = c("interval",
"vctrs_rcrd", "vctrs_vctr")), class = c("tbl_ts", "tbl_df", "tbl",
"data.frame"))
我没有把数字一路往下填,但希望这能说明我的想法。我试图得到当前日期的总平均销售额,每个新日期都需要额外一天的数据来计算总平均销售额。
2条答案
按热度按时间fcy6dtqo1#
在
dplyr
中使用cummean
fcy6dtqo2#