简体   繁体   English

如何创建提取月份和年份的表

[英]How to create table extracting month and year

I have data in the following format我有以下格式的数据

number_of_tickets : "01-01-2019", "02-01-2019", "03-01-2019"......
date              :  1500       ,  1200       , "2000......

It is past two years of data and I need to computer total tickets opened yearly and monthly, something like below这是过去两年的数据,我需要计算每年和每月打开的总票数,如下所示

      Jan     Feb    Mar....
2019  20570   18702  35078

2020  19794   11325  42723......

I am trying to using package lubridate and deplyr to summarize, mutate and many other things but not getting any headsway.我正在尝试使用 package lubridatedeplyr来总结、变异和许多其他事情,但没有取得任何进展。

Any help will be appreciated!!!!任何帮助将不胜感激!!!!

Thanks谢谢

I think you are looking for this我想你正在寻找这个

df <- data.frame(
  number_of_tickets = c(1500, 1200, 2000, 1000, 2000, 3000),
  date              = c("01-01-2019", "02-01-2019", "03-01-2019",
                        "01-01-2020", "02-01-2020", "03-01-2020"))


df$date <- as.Date(df$date, format = c("%d-%m-%Y"))

head(df)


df$month <- format(df$date, "%m")
df$year  <- format(df$date, "%y")


head(df)


aggregate(number_of_tickets ~ month + year,
          data = df,
          sum)

The output of the last call is最后一次调用的 output 是

  month year number_of_tickets
1    01   19              4700
2    01   20              6000

HTH HTH

with tidyverse and lubridate, you can also proceed使用 tidyverse 和 lubridate,您还可以继续

df <- data.frame(
  number_of_tickets = c(1500, 1200, 2000, 1000, 2000, 3000),
  date              = c("01-01-2019", "02-01-2019", "03-02-2019",
                        "01-01-2020", "02-01-2020", "03-02-2020"))

library(lubridate)
library(tidyverse)

df %>% mutate(month = month(as.Date(date, format = "%d-%m-%Y")),
              year = year(as.Date(date, format = "%d-%m-%Y"))) %>%
  pivot_wider(id_cols = "year", names_from = month, values_from = number_of_tickets, values_fn = sum)

# A tibble: 2 x 3
   year   `1`   `2`
  <dbl> <dbl> <dbl>
1  2019  2700  2000
2  2020  3000  3000

with pivottabler library带有数据透视表

library(pivottabler)
library(lubridate)

df$date <- as.Date(df$date, format = "%d-%m-%Y")
df$Month <- month(df$date)
df$Year <- year(df$date)


qpvt(df, rows = "Month", 
     columns = "Year", 
     calculations = "sum(number_of_tickets)")

       2019  2020  Total  
1      2700  3000   5700  
2      2000  3000   5000  
Total  4700  6000  10700

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM