如何分别正确地按周汇总每组的日期

发布于 2025-01-09 14:03:07 字数 3123 浏览 3 评论 0原文

dput()

a=structure(list(sales_point_id = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 15L, 15L, 15L, 15L, 15L, 15L), calendar_id_operday = c(20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L), line_fact_amt = c(23749.14, 
1000, 3050, 1550, 8900, 1550, 0, 300, 0, 499, 5450, 300, 0, 499, 
599, 599, 6050, 300, 599, 1400, 300, 0, 2000, 700, 0, 5990, 8877, 
1999, 257.5, 200, 361, 300, 1990, 2453, 3140, 0, 0, 199, 599, 
10990, 7990, 773, 400, 6000, 2269, 2000, 1999, 999, 300, 0, 200, 
11990, 300, 200, 3599, 200, 50, 100, 100, 100, 50, 0, 1836, 19749, 
1399, 1266, 1538, 6031, 34846, 200, 1799, 250, 899, 4049, 379, 
799, 200, 200, 200, 599, 999, 0, 0, 300, 300, 3499, 4211, 626, 
12801, 999, 899, 799, 299, 1218, 200, 99, 999, 590, 200, 17990
)), row.names = c(NA, 100L), class = "data.frame")

calendar_id_operday 是日期列。格式 20210102 为(年|月|日)。 我需要按周分别汇总每个 sales_point_id 的 line_fact_amt 列。 我尝试这样做

df2<-a %>% group_by( weekk = week(ymd(calendar_id_operday)), line_fact_amt ) %>% 
  summarise(new = sum(sales_point_id))

,但我得到不需要的结果,

   weekk line_fact_amt   new
   <dbl>         <dbl> <int>
 1     1            0     40
 2     1           50     10
 3     1           99     15
 4     1          100     15
 5     1          199      2
 6     1          200     76
 7     1          250      8
 8     1          258.     2
 9     1          299      8
10     1          300     36

我需要这样的结果

sales_point_id  calendar_id_operday line_fact_amt
1   20210102    100
1   20210109    200

,其中 100 和 200 是 line_fact_amt 的 sales_point_id 的一周总和。 我怎样才能得到需要的结果?

dput()

a=structure(list(sales_point_id = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 15L, 15L, 15L, 15L, 15L, 15L), calendar_id_operday = c(20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 20210102L, 
20210102L, 20210102L, 20210102L), line_fact_amt = c(23749.14, 
1000, 3050, 1550, 8900, 1550, 0, 300, 0, 499, 5450, 300, 0, 499, 
599, 599, 6050, 300, 599, 1400, 300, 0, 2000, 700, 0, 5990, 8877, 
1999, 257.5, 200, 361, 300, 1990, 2453, 3140, 0, 0, 199, 599, 
10990, 7990, 773, 400, 6000, 2269, 2000, 1999, 999, 300, 0, 200, 
11990, 300, 200, 3599, 200, 50, 100, 100, 100, 50, 0, 1836, 19749, 
1399, 1266, 1538, 6031, 34846, 200, 1799, 250, 899, 4049, 379, 
799, 200, 200, 200, 599, 999, 0, 0, 300, 300, 3499, 4211, 626, 
12801, 999, 899, 799, 299, 1218, 200, 99, 999, 590, 200, 17990
)), row.names = c(NA, 100L), class = "data.frame")

calendar_id_operday is date column. Format 20210102 is (year|mon|day).
I need aggregate line_fact_amt column by week for each sales_point_id separately by sum.
I try do so

df2<-a %>% group_by( weekk = week(ymd(calendar_id_operday)), line_fact_amt ) %>% 
  summarise(new = sum(sales_point_id))

but i get not needed result

   weekk line_fact_amt   new
   <dbl>         <dbl> <int>
 1     1            0     40
 2     1           50     10
 3     1           99     15
 4     1          100     15
 5     1          199      2
 6     1          200     76
 7     1          250      8
 8     1          258.     2
 9     1          299      8
10     1          300     36

I need result like this

sales_point_id  calendar_id_operday line_fact_amt
1   20210102    100
1   20210109    200

where 100 and 200 is sum for week for this sales_point_id by line_fact_amt.
How can i get needed result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话 2025-01-16 14:03:07

您必须对 line_fact_amt 和 group_by sales_point_id 求和,但不能按 line_fact_amt 求和。

a %>% 
  group_by(sales_point_id, week = week(ymd(calendar_id_operday))) %>% 
  summarise(line_fact_amt = sum(line_fact_amt))

# A tibble: 4 x 3
# Groups:   sales_point_id [4]
  sales_point_id  week line_fact_amt
           <int> <dbl>         <dbl>
1              2     1       118181.
2              5     1        85053 
3              8     1        36324 
4             15     1        20078 

You have to sum over line_fact_amt and also group_by sales_point_id but not by line_fact_amt.

a %>% 
  group_by(sales_point_id, week = week(ymd(calendar_id_operday))) %>% 
  summarise(line_fact_amt = sum(line_fact_amt))

# A tibble: 4 x 3
# Groups:   sales_point_id [4]
  sales_point_id  week line_fact_amt
           <int> <dbl>         <dbl>
1              2     1       118181.
2              5     1        85053 
3              8     1        36324 
4             15     1        20078 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文