R 错误消息 - "arguments imply differing number of rows"

标签 r ggplot2 statistics time-series

我有 1970 年至 2019 年行驶里程的数据,每个条目都是一个新的月份。
该数据集中没有缺失数据。
以下是 CSV 格式的数据片段:

DATE,TRFVOLUSM227NFWA
1970-01-01,80173
1970-02-01,77442
1970-03-01,90223
1970-04-01,89956
1970-05-01,97972
1970-06-01,100035
1970-07-01,106392
1970-08-01,106969
1970-09-01,95254
1970-10-01,96317
1970-11-01,89684
1970-12-01,89911
1971-01-01,85336
1971-02-01,80118
1971-03-01,92974
1971-04-01,98106
1971-05-01,103655
1971-06-01,105433
1971-07-01,112466
1971-08-01,112642
1971-09-01,101290
1971-10-01,102525
1971-11-01,95555
1971-12-01,95515

现在我正在尝试使用 ggplot 库创建月图:

library(dplyr) # data management
library(tidyr) # data management
library(ggplot2) # visualization
library(gridExtra) # combine multiple plots
library(stats) # basic statistical tools
library(forecast) # main time series analysis package
library(lubridate) # data formatting
library(tseries) # addition to forecast
library(zoo) # z-ordered observations (irregular timeseries)

data = read.csv("Travelling_Statistics.csv")
timeseries = ts(data,start=c(1970,1),frequency=12) # time series in monthly frequency from January 1970
ggmonthplot(timeseries)

我收到以下错误:

Error in data.frame(y = as.numeric(x), year = trunc(time(x)), season = as.numeric(phase)) : arguments imply differing number of rows: 1176, 588

此错误消息的原因可能是什么以及如何修复?
我在网上找到的所有内容似乎都与我遇到的问题无关。
提前致谢!

最佳答案

问题在于 ts 无法完全正确识别您的时间序列。当您传递开始值和频率值时,您不需要(不能)也将日期传递给 ts,否则 ts 不知道如何处理日期列。

因此,只需将值列转换为时间序列即可:

library(forecast)

df <- read.table(text= "DATE,TRFVOLUSM227NFWA
1970-01-01,80173
1970-02-01,77442
1970-03-01,90223
1970-04-01,89956
1970-05-01,97972
1970-06-01,100035
1970-07-01,106392
1970-08-01,106969
1970-09-01,95254
1970-10-01,96317
1970-11-01,89684
1970-12-01,89911
1971-01-01,85336
1971-02-01,80118
1971-03-01,92974
1971-04-01,98106
1971-05-01,103655
1971-06-01,105433
1971-07-01,112466
1971-08-01,112642
1971-09-01,101290
1971-10-01,102525
1971-11-01,95555
1971-12-01,95515", sep = ",", header = TRUE)

timeseries = ts(df$TRFVOLUSM227NFWA,start=c(1970,1),frequency=12) # time series in monthly frequency from January 1970

ggmonthplot(timeseries)

enter image description here

关于R 错误消息 - "arguments imply differing number of rows",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72583918/

相关文章:

r - 转换简单的ggplot2代码以使用data.table

r - 如何在 RMarkdown html 中为 ggplot 图添加水平滚动条

c# - 使用 LINQ 创建增量值的 IEnumerable<>

r - 如何在 R 中堆叠具有几乎完全相同范围的栅格

r - 将两个边距添加到 facet_grid 但不是组合

r - 将 NA 值的框添加到 ggplot 图例以获得连续 map

image-processing - 胡七不变矩在物体识别中的意义

windows - R 脚本自动化时的不同结果

r - 具有连续彩虹颜色的热图

statistics - 如何从 bittorrent swarm 中收集统计信息?