我有一组销售报告,其中包含报告“每天”或“每月”销售数字的商店。 当我将它们绘制在同一张图上时,“每月”图看起来像尖峰,这使得该图难以理解。
我希望将这些“每月一次”的数字转换为一个月中的几天均匀分布,以便绘制每日销售图表。
我设法使用tidyverse,lubridate来计算数据集中的“ sales_per_day”列。如何创建“每天1行”的行,即对于2019-01年,是否从每1行每月数据中创建30条每日行?
sales <- tibble(
distributor = c("StoreA", "StoreA", "StoreA", "StoreA", "StoreB"),
sales = c(100,200,300,400,5000),
date = c("2019-01-01", "2019-01-02", "2019-01-03", "2019-01-04", "2019-01-30"),
freq = c("daily", "daily", "daily", "daily", "monthly"))
> sales
# A tibble: 5 x 4
distributor sales date freq
<chr> <dbl> <chr> <chr>
1 StoreA 100 2019-01-01 daily
2 StoreA 200 2019-01-02 daily
3 StoreA 300 2019-01-03 daily
4 StoreA 400 2019-01-04 daily
5 StoreB 5000 2019-01-30 monthly
wanted_sales <- tibble(
distributor = c("StoreA", "StoreA", "StoreA", "StoreA", "StoreB", "StoreB", "StoreB", "StoreB"),
sales = c(100, 200, 300, 400, 5000 / 30, 5000 / 30, 5000 / 30, 5000 / 30),
date = c("2019-01-01", "2019-01-02", "2019-01-03", "2019-01-04", "2019-01-01", "2019-01-02", "2019-01-03", "2019-01-04"),
freq = c("daily", "daily", "daily", "daily", "daily", "daily", "daily", "daily" ))
> wanted_sales
# A tibble: 8 x 4
distributor sales date freq
<chr> <dbl> <chr> <chr>
1 StoreA 100 2019-01-01 daily
2 StoreA 200 2019-01-02 daily
3 StoreA 300 2019-01-03 daily
4 StoreA 400 2019-01-04 daily
5 StoreB 167. 2019-01-01 daily
6 StoreB 167. 2019-01-02 daily
7 StoreB 167. 2019-01-03 daily
8 StoreB 167. 2019-01-04 daily
per_day <- sales %>% filter(freq == "monthly") %>%
group_by(date) %>%
mutate(mdays = as.integer(days_in_month(as_date(date)))) %>%
mutate(sales_per_day = sales / mdays)
> per_day
# A tibble: 1 x 6
# Groups: date [1]
distributor sales date freq mdays sales_per_day
<chr> <dbl> <chr> <chr> <int> <dbl>
1 StoreB 5000 2019-01-30 monthly 31 161.
我希望使最终的per_day带有30行,并且$ date列是“ 2019-01-01”,“ 2019-01-02” ...“ 2019-01-30”的序列。
答案 0 :(得分:2)
我们可以将let
更改为实际的Date类,并创建一个新列date
,如果startdate
不是freq
且{ {1}}除以30。对于每个"daily"
,我们使用sales
创建日期序列并将所有date
更改为complete
。
freq