在R中创建时间序列图时出错:plot.window中的错误(xlim,ylim,log,...):需要有限的'ylim'值

时间:2015-09-01 20:04:09

标签: r time-series data-analysis

以下是我的单列数据集的示例:

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对象。

1 个答案:

答案 0 :(得分:0)

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