R语言 对列中的多个项目求和

ibps3vxo  于 2022-12-05  发布在  其他
关注(0)|答案(3)|浏览(151)
Type                Date        Cost
Shampoo          01/31/2022     $10
Shampoo          01/31/2022     $15
Shampoo          02/22/2019     $15
Conditioner      03/15/2020     $17
Conditioner      05/16/2022     $19
Soap.            01/31/2021     $5
Soap             01/06/2022     $2
Soap             12/31/2019     $3
Soap             10/10/2022     $5

我如何计算一年、一个月、一个季度中特定项目的总成本和总成本
输出示例:

Type     | Number Items | Year | Total Cost 
Shampoo  |   2          | 2022 |    25
Shampoo  |  1.          | 2019 |   15

等等。
按月和季度拆分
尝试汇总和library(lubridate)

3pvhb19x

3pvhb19x1#

library(tidyverse)
library(lubridate)

df %>%
   group_by(Type, Date)%>%
   summarise(Number_Items = n(),
             Year = year(mdy(Date[1])),
             Total_Cost = sum(parse_number(Cost)),
             .groups = 'drop')

# A tibble: 8 × 5
  Type        Date       Number_Items  Year Total_Cost
  <chr>       <chr>             <int> <dbl>      <dbl>
1 Conditioner 03/15/2020            1  2020         17
2 Conditioner 05/16/2022            1  2022         19
3 Shampoo     01/31/2022            2  2022         25
4 Shampoo     02/22/2019            1  2019         15
5 Soap        01/06/2022            1  2022          2
6 Soap        10/10/2022            1  2022          5
7 Soap        12/31/2019            1  2019          3
8 Soap.       01/31/2021            1  2021          5
w9apscun

w9apscun2#

你必须按类型和年份对你的数据进行分组,然后计算物品的数量和总成本,这里有一个例子供你适应。

library(dplyr)
library(lubridate)

your_data_frame %>% 
  group_by(type, year = year(dmy(Date))) %>% 
  summarise(
    number_of_items = n(),
    total_cost = sum(cost,na.rm = TRUE)
  )
ryoqjall

ryoqjall3#

这一个与@onyambu的解决方案非常相似,但在分组上有所不同:
第一个

相关问题