绘制原子矢量时出错

时间:2016-08-22 14:28:26

标签: r plot time-series

我想绘制日志返回图。我以CSV格式导入了我的数据文件。之后的代码如下所示,错误。有关更多信息,表中存在所有变量。

t <- read.csv("~/Documents/FYP/Log return 1.csv")
View(t)
df<-ts(t)
plot.ts(df$Year,df$IND)
  

df $ Year出错:$ operator对原子向量无效

plot.ts(df[Time],df[CHN])
  

NextMethod错误(&#34; [&#34;):对象&#39;时间&#39;找不到

plot.ts(df[Year],df[CHN])
  

NextMethod错误(&#34; [&#34;):对象&#39;年份&#39;找不到

plot.ts(df[[Year]],df[[CHN]])
  

NCOL(x)中的错误:对象&#39;年&#39;找不到

1 个答案:

答案 0 :(得分:1)

给定数据框输入tdf <- ts(t)会为您提供矩阵而不是数据框,因此使用$无效。要访问矩阵的列,您需要 例如,df[, "Time"]

举个例子,让我们使用R的内置数据集cars。最初它是一个包含两列的数据框:speeddist,而x <- ts(cars)给出一个矩阵:

class(x)
# [1] "mts"    "ts"   "matrix"

head(x)
#     speed dist
#[1,]     4    2
#[2,]     4   10
#[3,]     7    4
#[4,]     7   22
#[5,]     8   16
#[6,]     9   10

您看到的错误可以通过

重现
x$dist
# Error in x$dist : $ operator is invalid for atomic vectors

相反,我们想要

x[, "dist"]
#Time Series:
#Start = 1 
#End = 50 
#Frequency = 1 
# [1]   2  10   4  22  16  10  18  26  34  17  28  14  20  24  28  26  34  34  46
#[20]  26  36  60  80  20  26  54  32  40  32  40  50  42  56  76  84  36  46  68
#[39]  32  48  52  56  64  66  54  70  92  93 120  85