FaCoffee FaCoffee -4 years ago 117
R Question

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?

Answer Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download