【问题标题】:Error while creating a Timeseries plot in R: Error in plot.window(xlim, ylim, log, ...) : need finite 'ylim' values在 R 中创建时间序列图时出错:plot.window(xlim, ylim, log, ...) 中的错误:需要有限的 'ylim' 值
【发布时间】:2015-11-27 04:26:58
【问题描述】:

这是我的单列数据集的示例:

Lines 
141,523
146,785
143,667
65,560
88,524
148,422

我将此文件读取为 .csv 文件,将其转换为 ts 对象,然后将其绘制:

##Read the actual number of lines CSV file

Aclines <- read.csv(file.choose(), header=T, stringsAsFactors = F)

Aclinests <- ts(Aclines[,1], start = c(2013), end = c(2015), frequency = 52)

plot(Aclinests, ylab = "Actual_Lines", xlab = "Time", col = "red")

我收到以下错误消息:

Error in plot.window(xlim, ylim, log, ...) : need finite 'ylim' values
In addition: Warning messages:
1: In xy.coords(x, NULL, log = log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

我认为这可能是因为列中的“,”并尝试使用 sapply 来解决此问题,如下所示:

need finite 'ylim' values-error

plot(sapply(Aclinests, function(x)gsub(",",".",x)))

但我收到以下错误:

Error in plot(sapply(Aclinests, function(x) gsub(",", ".", x))) : 
  error in evaluating the argument 'x' in selecting a method for function 'plot': Error in sapply(Aclinests, function(x) gsub(",", ".", x)) : 
  'names' attribute [105] must be the same length as the vector [1]

如果可能有帮助,这是我的原始数据集和 ts 数据集的头部:

> head(Aclines)
    Lines
1 141,523
2 146,785
3 143,667
4  65,560
5  88,524
6 148,422
> head(Aclinests)
[1] "141,523" "146,785" "143,667" "65,560"  "88,524"  "148,422"

另外,如果我将 .csv 文件读取为:

Aclines <- read.csv(file.choose(), header=T, **stringsAsFactors = T**)

然后,我可以绘制 ts 对象,但head(Aclinests)给出的输出与我的原始数据不一致:

> head(Aclinests)
[1] 14 27 17 84 88 36

请建议我如何绘制这个 ts 对象。

【问题讨论】:

  • 您是否尝试将as.numeric() 添加到您对sapply 的呼叫中,即plot(sapply(Aclinests, function(x) as.numeric(gsub(",",".",x))))
  • 我收到以下错误消息:Error in plot(sapply(Aclinests, function(x) as.numeric(gsub(",", ".", : error in evaluating the argument 'x' in selecting a method for function 'plot': Error in sapply(Aclinests, function(x) as.numeric(gsub(",", ".", x))) : 'names' attribute [105] must be the same length as the vector [1]
  • 如果您读入一个 2 列数据文件,标题中只有一项,那么只有一列数据。标题之后的行中的第一项将作为行号出现。

标签: r time-series data-analysis


【解决方案1】:

就我而言,避免这种情况的最简单方法是删除包含数据的 excel 文件中的逗号。这可以使用简单的 excel 命令来完成,它对我有用。

【讨论】:

  • 这并不是问题的真正答案。我建议你应该删除这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 2014-02-16
相关资源
最近更新 更多