简体   繁体   中英

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. 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

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

Error in NextMethod("[") : object 'Time' not found

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

Error in NextMethod("[") : object 'Year' not found

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

Error in NCOL(x) : object 'Year' not found

Given a data frame input t , df <- ts(t) gives you a matrix rather than a data frame, so using $ is invalid. To access a column of a matrix, you need for example, df[, "Time"] .

As an example, let's use R's built-in dataset cars . Originally it is a data frame with two columns: speed and dist , while x <- ts(cars) gives a matrix:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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