FaCoffee
FaCoffee

Reputation: 7909

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?

Upvotes: 1

Views: 48

Answers (2)

FaCoffee
FaCoffee

Reputation: 7909

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.

Upvotes: 1

lmo
lmo

Reputation: 38510

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"

Upvotes: 1

Related Questions