Merging zoo object using do.call in R


A have several csv file like this :

,timestamp,AirTemperature_House
    1,2013-09-01 00:00:00,8.22
    2,2013-09-01 01:00:00,6.53
    3,2013-09-01 02:00:00,6.67
    4,2013-09-01 03:00:00,5.58
    5,2013-09-01 04:00:00,4.16
    6,2013-09-01 05:00:00,4.76
    7,2013-09-01 06:00:00,5.06
    8,2013-09-01 07:00:00,5.16
    9,2013-09-01 08:00:00,6.83
    10,2013-09-01 09:00:00,8.59
    11,2013-09-01 10:00:00,10.99
    12,2013-09-01 11:00:00,11.08

I grouped them to a list of zoo object using the following code :

raw_data<-list.files(path = "./AWS_Data_STU/Air_temp/",pattern="Air",full.names = T)

data_stu<-lapply(raw_data,function(x){
   ss<-read.csv(x)

  ss<-zoo(ss,order.by = ss$timestamp)


})

I made a list of zoo object which all look like this one :

str(data_stu[[1]])

    ‘zoo’ series from 2013-09-01 00:00:00 to 2014-04-30 23:00:00
      Data: num [1:5808] 8.22 6.53 6.67 5.58 4.16 4.76 5.06 5.16 6.83 8.59 ...
      Index:  Factor w/ 5808 levels "2013-09-01 00:00:00",..: 1 2 3 4 5 6 7 8 9 10

...

I want to merge all my list to a data frame as :

X1  x2  x3  X4 x5 x6  x7
1 12.95    NA NA NA    
2 14.81 14.37 NA NA 12.78 NA 
3 15.02 15.11 NA NA 12.61 NA 
4 13.91 14.25 NA NA 11.89 NA 
5 12.34 13.96 NA NA 10.86 NA 
6 14.40 14.47 NA NA 10.40 NA 

I used the do call function

do.call(merge.zoo,data_stu )

    structure(c(7.66, 7.29, 7.34, 7.15, 6.76, 6.41, 6.25, 6.36, 6.78, 
    1                                                                    NA
    2                                                                    NA
    3                                                                    NA
    4                                                                    NA
    5                                                                    NA
    6                                                                    NA
    7                                                                    NA
    8                    

but it gave me only NA object.

Any ideas ?


Answers:


The problem is that the index of all your zoo objects are factors. You need to convert them to POSIXct. Also, you should not call methods directly. I.e., you should call merge instead of merge.zoo and let R handle method dispatch.

You can also use read.zoo to help with the conversion.

data_stu <- do.call(merge, lapply(raw_data, read.zoo, sep=",", header=TRUE,
    FUN=as.POSIXct, colClasses=c("NULL", "character", "numeric")))