简体   繁体   English

绘制原子矢量时出错

[英]Error in plotting atomic vector

I want to plot log return graph. 我想绘制日志返回图。 I imported my data file in CSV format.My code after that as below with the errors. 我以CSV格式导入了数据文件,之后的代码如下并出现错误。 For more information all the variables do exist in the table. 有关更多信息,表中确实存在所有变量。

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

Error in df$Year : $ operator is invalid for atomic vectors df $ Year中的错误:$运算符对原子向量无效

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

Error in NextMethod("[") : object 'Time' not found NextMethod(“ [”)中的错误:找不到对象'Time'

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

Error in NextMethod("[") : object 'Year' not found NextMethod(“ [”)中的错误:找不到对象'Year'

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

Error in NCOL(x) : object 'Year' not found NCOL(x)中的错误:找不到对象“年份”

Given a data frame input t , df <- ts(t) gives you a matrix rather than a data frame, so using $ is invalid. 给定数据帧输入tdf <- ts(t)给您矩阵而不是数据帧,因此使用$无效。 To access a column of a matrix, you need for example, df[, "Time"] . 要访问矩阵的一列,您需要例如df[, "Time"]

As an example, let's use R's built-in dataset cars . 例如,让我们使用R的内置数据集cars Originally it is a data frame with two columns: speed and dist , while x <- ts(cars) gives a matrix: 最初,它是一个具有两列的数据帧: 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

The error you saw can be reproduced by 您看到的错误可以通过以下方式重现

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

Instead, we want 相反,我们想要

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM