0
votes

I have a very large timeseries(xts) data frame variables (>10) with varying number of rows,days and starting and ending dates. I want to combine these dataframe as a single time series dataframe.

For clarity I would give an example; consider A, B and C time series data frame with 100,200 and 300 rows of dates respectively and one column for its corresponding values.

A              Result                B          Result      C               Result
2014-02-01  0.8478517865        2016-03-01  0.794655429     2014-02-01  0.5961746441
2015-03-02  0.8310818302        2016-03-02  0.4288015561    2014-08-03  0.4332428675
2015-04-13  0.6525838461        2016-03-04  0.8032966915    2015-03-01  0.4675749368
2015-04-27  0.0078298878        2016-03-06  0.588762206     2015-03-02  0.6404606516
2015-05-05  0.4810352649        2016-03-08  0.8551481313    2016-03-01  0.403449801
2015-05-06  0.2730398192        2016-03-10  0.7437164122    2016-03-09  0.1844344875
2015-05-07  0.5594211367        2016-03-11  0.1973790985            
2015-05-08  0.1888440552        2016-03-13  0.5973634648            
2015-05-09  0.8211225735                        
2015-05-10  0.2937804316                        
2015-05-11  0.4311328372                        

How can I combine these three time series data frames with one columns as a single data frame with 3 columns?

I have tried to use cbind(A,B) but its giving thise error

Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows:

Also tried this answer using rbind.fill(A,B) but its appending the time series data frame

row of A=100 with one column

row of B=200 with one column

after rbind.fill number of rows 300 column 1

Edited

This code is obtained from this answer.

cbind.fill <- function(...){
    nm <- list(...) 
    nm <- lapply(nm, as.matrix)
    n <- max(sapply(nm, nrow)) 
    do.call(cbind, lapply(nm, function (x) 
        rbind(x, matrix(, n-nrow(x), ncol(x))))) 
}

This function combines two xts dataframe but it doesn't do a proper alignment of data with dates

comb-cbind.fill(A,B)
head(comb)
1998-01-02 "0.332" "0.849"
1998-01-05 "0.227" "0.060"
1998-01-06 "0.394" "0.071"
1998-01-07 "0.422" "6.066"
tail(comb)
 NA    "0.306"
 NA    "0.479"
 NA    "0.127"
 NA    "0.321"
2
you can not have a dataframe where columns are of varying length ,Bg1850
You might want to use a list in this caseBg1850
You say you have several "xts data frame variables". That's not possible. They must be either xts objects or data.frame objects. xts objects are a matrix with an index attribute; they cannot be a data.frame. If you really do have 3 xts objects, the answer is simple: merge(A, B, C).Joshua Ulrich
@JoshuaUlrich Sorry for any confusion as english is not my first language. As you are working in quantmod I will give a similar example of my variables A<-Cl(aapl), B<-Cl(goog), C<-Cl(f), D..Eka
You should not edit the solution into your question (I have removed it). You can answer your own question and mark it as accepted.Joshua Ulrich

2 Answers

1
votes

As Joshua suggested I will write solution for my problem.

The problem was with one of my code itself which converted my time series to a data frame. I debugged the problem by using is.data.frame()

is.data.frame(A)
TRUE

removing the problem causing function solved the error and then I combined varying length time series using cbind(A,B)

0
votes

Probably we could use merge. Since time columns are used as merging index, then:

merge(A,B, all = TRUE, by.x = "A", by.y = "B")

If all dates in A$A column (100) are members of B$B columns (200), then you will get 200 rows with 3 column (1 date and 2 data). I assume the dates are stored in a separate column.