簡體   English   中英

將 R dataframe 轉換為時間序列

[英]Transforming R dataframe into time series

我從消費者投訴數據庫中提取了一組數據。 但是,我很難將其轉換為時間序列,特別是因為在同一時間范圍內報告了相同的問題(不是唯一的)。 我的最終目標是將問題的頻率與 plot 行中按月組織的時間范圍進行比較。

以下是來自總共超過 750,000 個條目的data.frame的前 5 行:

Date        Issue 
08/25/14    Making/receiving payments, sending money None   
04/20/17    Other       
02/14/14    Billing disputes
08/30/13    Managing the loan or lease  
10/03/14    Billing disputes    
01/07/13    Billing disputes

像這樣的東西?

df <- data.frame(stringsAsFactors=FALSE,
              Date = sample(c("08/25/14", "04/20/17", "02/14/14", "08/30/13", "10/03/2014",
                       "1/07/2013"), 100, replace = TRUE),
             Issue = sample(c("Making/receiving", "Other", "Billing", "Managing", "Billing",
                       "Billing"), 100, replace = TRUE)
      )

library(lubridate)
library(dplyr)
library(ggplot2)

df <- df %>% 
    mutate(
        Date = mdy(Date),
        Year = year(Date),
        Month = month(Date),
        Period = make_date(Year, Month, 1)
    ) %>% 
    group_by(Period, Issue) %>% 
    summarise(
        incidents = n()
    ) 

ggplot() +
    geom_path(data = df, mapping = aes(x = Period, y = incidents, colour = Issue))

代表 package (v0.3.0) 於 2019 年 11 月 19 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM