将具有日期数据的csv文件聚合到月中

问题描述 投票:1回答:2

我有一个名为crime.csv的csv文件如下: -

OFFENSE_CODE    OFFENSE_TYPE        OFFENSE_DESCRIPTION             DISTRICT    DAY YEAR    MONTH   STREET          NO_OF_CRIME
1106            Confidence Games    FRAUD - CREDIT CARD / ATM FRAUD     B2      1   2018    2       WASHINGTON ST   1
3201            Property Lost       PROPERTY - LOST                     B2      15  2018    3       ELM HILL AVE    1
1001            Counterfeiting      FORGERY / COUNTERFEITING            D4      9   2018    1       TREMONT ST      1
2629            Harassment          HARASSMENT                          E5      1   2018    1       CROWN POINT DR  1
1001            Counterfeiting      FORGERY / COUNTERFEITING            E5      8   2018    4       REDGATE RD      1
1106            Confidence Games    FRAUD - CREDIT CARD / ATM FRAUD     D4      22  2018    2       BOYLSTON ST     1
2629            Harassment          HARASSMENT                          B2      9   2017    10      AKRON ST        1
1102            Fraud               FRAUD - FALSE PRETENSE / SCHEME     A7      25  2018    4       LIVERPOOL ST    1
3201            Property Lost       PROPERTY - LOST                     D14     1   2018    1       FIDELIS WAY     1
1106            Confidence Games    FRAUD - CREDIT CARD / ATM FRAUD     E5      12  2018    4       SPRING ST       1
3201            Property Lost       PROPERTY - LOST                     A1      30  2018    4       NASHUA ST       1

我需要根据OFFENCE_CODE将这些数据汇总到月度数据中。这样NO_OF_CRIMES会在特定月份汇总。任何帮助都会非常棒。

r csv date aggregate
2个回答
0
投票

假设x是数据帧的名称:

aggregate(x$NO_OF_CRIME, by = list(x$OFFENCE_CODE, x$MONTH), FUN = sum)

0
投票

我们可以使用tidyverse

library(tidyverse)
df1 %>%
   group_by(OFFENCE_CODE, YEAR, MONTH) %>%
   mutate(SUM_NO_OF_CRIME = sum(NO_OF_CRIME))
© www.soinside.com 2019 - 2024. All rights reserved.