将 R 数据帧转换为时间序列
Transforming R dataframe into time series
我从消费者投诉数据库中提取了一组数据。但是,我很难将其转换为时间序列,尤其是因为在同一时间范围内报告了相同的问题(不是唯一的)。我的最终目标是将问题的频率与线图中按月组织的时间范围进行比较。
以下是总计超过 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))
由 reprex package (v0.3.0)
于 2019-11-19 创建
我从消费者投诉数据库中提取了一组数据。但是,我很难将其转换为时间序列,尤其是因为在同一时间范围内报告了相同的问题(不是唯一的)。我的最终目标是将问题的频率与线图中按月组织的时间范围进行比较。
以下是总计超过 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))
由 reprex package (v0.3.0)
于 2019-11-19 创建