简体   繁体   中英

R: reading csv files from a directory with zoo returns logical instead of time series

I have two csv files in a folder. My files look like this:

file1.csv   file2.csv
1           10
2           20
3           30
4           40
5           50
6           60
7           70

I need to read these csv in and turn them into time series ts for further analysis with stl . My attempt:

dir<-"C:\\Users\\MyName\\Desktop\\MyFolder"
setwd(dir)

library(zoo)

listcsv<-dir(pattern="*.csv")
z1 <- read.zoo(file = listcsv[1], sep = ",", header = FALSE)
z2 <- read.zoo(file = listcsv[2], sep = ",", header = FALSE)

returns two elements that are of the type logical instead of ts :

> typeof(z1)
[1] "logical"
> typeof(z2)
[1] "logical"

What am I doing wrong?

typeof will return the data type of the atomic vector in your ts. To check if it is a ts , use class . Here's a reproducible example

# arbitrary time series
temp <- ts(1:5)

temp
Time Series:
Start = 1 
End = 5 
Frequency = 1 
[1] 1 2 3 4 5

typeof(temp)
[1] "integer"

class(temp)
[1] "ts"

A little research goes a long way.

To turn the csv files into time series, it seems like it is ok to do:

z3<-ts(z1)
z4<-ts(z2)

so that:

> z3
Time Series:
Start = 1 
End = 7 
Frequency = 1 

1
2
3
4
5
6
7
attr(,"index")
[1] 1 2 3 4 5 6 7

and

> class(z3)
[1] "ts"

The same of course applies to z4 .

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