-2

I need to merge two time series, and it seems it's best to do it using merge.zoo. When I try to convert each of the two data frames to zoo, I get the following error:

zoo(FNCC_short)  
Error in as.matrix.data.frame(x) :   
dims [product 10] do not match the length of object [19]

Where did R find length of 19? I have:

> class(FNCC_short)  
[1] "data.frame"  
> length(FNCC_short)  
[1] 2  
> length(FNCC_short[,1])  
[1] 10  
> length(FNCC_short[,2])  
[1] 10  

I could not use read.zoo(...) to get the data because the csv time fields are not in the right format - I had to read the csv file first, then fix the time format.

Here is the FNCC_short:

> FNCC_short  
             time_FNCC  FNCC  
1  2013-02-07 09:00:00 2.556  
2  2013-02-07 09:01:00 2.556  
3  2013-02-07 09:02:00 2.552  
4  2013-02-07 09:03:00 2.552  
5  2013-02-07 09:04:00 2.552  
6  2013-02-07 09:05:00 2.552  
7  2013-02-07 09:06:00 2.552  
8  2013-02-07 09:07:00 2.539  
9  2013-02-07 09:08:00 2.539  
10 2013-02-07 09:09:00 2.539  

> class(FNCC_short$time_FNCC)  
[1] "POSIXlt" "POSIXt"   

So, the first column it POSIX time. Why doesn't this simple conversion work?

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
user2069819
  • 53
  • 1
  • 5

1 Answers1

1

you can use specify format option to convert the first and the second column to a zoo index.

Lines <- 'time_FNCC time FNCC  ## I had to add the 'time' column name here
1  2013-02-07 09:00:00 2.556  
2  2013-02-07 09:01:00 2.556  
3  2013-02-07 09:02:00 2.552  
4  2013-02-07 09:03:00 2.552  
5  2013-02-07 09:04:00 2.552  
6  2013-02-07 09:05:00 2.552  
7  2013-02-07 09:06:00 2.552  
8  2013-02-07 09:07:00 2.539  
9  2013-02-07 09:08:00 2.539  
10 2013-02-07 09:09:00 2.539'

dat <- read.zoo(text = Lines, index = 1:2, tz = "")

dat
2013-02-07 09:00:00 2013-02-07 09:01:00 2013-02-07 09:02:00 2013-02-07 09:03:00 
              2.556               2.556               2.552               2.552 
2013-02-07 09:04:00 2013-02-07 09:05:00 2013-02-07 09:06:00 2013-02-07 09:07:00 
              2.552               2.552               2.552               2.539 
2013-02-07 09:08:00 2013-02-07 09:09:00 
              2.539               2.539 
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Unfortunately, this does not solve my problem with data.frame. Actual data set is much longer, so it is not practical to transform it to text. I don't see a 'format' option in zoo( ), only in read.zoo( ), which does not accept a data.frame. – user2069819 Feb 22 '13 at 18:56
  • 1
    @user2069819 My answer was for your statement *I could not use read.zoo(...) to get the data because the csv time fields are not in the right format* . No you can. Now I think you mean , you have already read your data and you want re-raed it using read.zoo? can you `dput(head(data_frame))` to see why you can't convert it to zoo? – agstudy Feb 22 '13 at 19:01
  • user2069819, The answer used `text=` to make it reproducible. You can use the same command replacing 'text=...' with 'myfile.txt', say if you want to read it from a file. There is an entire vignette on using `read.zoo` and also examples in `?read.zoo`. Suggest you review these materials now. – G. Grothendieck Feb 22 '13 at 19:29
  • Ok, thanks for clarification. I found a roundabout involving Excel (if anyone is interested): if you use Time format in Excel of the type "**2/7/13 13:32**", then read.zoo("file.csv", format="%m/%d/%y %H:%M", sep=",", tz="", header=T) will read the data into zoo without problems. My problem was that original Excel format contained 'AM' in the time field, which zoo could not understand. sep=",", tz="", header=T) – user2069819 Feb 22 '13 at 19:44