wizgot wizgot - 3 months ago 18
R Question

Merging time series data in R zoo, joins are malformed

I am new to R, trying to merge 4 datasets using zoo, 2 datasets merge perfectly fine.

However 2 other data sets give me o/p as follows:

tmpA



2016-08-09 23:59:55 21.703
2016-08-09 23:59:56 21.703
2016-08-09 23:59:57 21.703
2016-08-09 23:59:58 21.703
2016-08-09 23:59:59 21.703
2016-08-10 00:00:00 21.703


tmpB



2016-08-09 23:59:52 24.600
2016-08-09 23:59:53 24.600
2016-08-09 23:59:54 24.600
2016-08-09 23:59:55 24.600
2016-08-09 23:59:56 24.600
2016-08-09 23:59:57 24.599


Code



infoA.zoo <- read.zoo(infoA.dfH, header = TRUE, tz = "")
infoB.zoo <- read.zoo(infoB.dfH, header = TRUE, tz = "")
#dfH is data.frame
#I am discretizing it over 1 sec.
merged = merge( tmpA, tmpB)


Output



Date A B
2016-08-09 23:59:56 NA 24.600
2016-08-09 23:59:57 21.703 NA
2016-08-09 23:59:57 NA 24.599
2016-08-09 23:59:58 21.703 NA
2016-08-09 23:59:59 21.703 NA
2016-08-10 00:00:00 21.703 NA

Answer

I try to reproduce your issue, but everything works fine. The only thing I changed was, that I change in the tmpA and tmpB the seperator from space to ';' and quote the date string. Furthermore I add the parameter sep=';' to the read.zoo function.

tmpA

"2016-08-09 23:59:55";21.703 
"2016-08-09 23:59:56";21.703 
"2016-08-09 23:59:57";21.703 
"2016-08-09 23:59:58";21.703 
"2016-08-09 23:59:59";21.703 
"2016-08-10 00:00:00";21.703 

tmpB

"2016-08-09 23:59:52";24.600
"2016-08-09 23:59:53";24.600
"2016-08-09 23:59:54";24.600
"2016-08-09 23:59:55";24.600
"2016-08-09 23:59:56";24.600
"2016-08-09 23:59:57";24.599

R

# Copy tmpA string to clipboard
X <- read.zoo('clipboard', tz="", sep=';')
# Copy tmpY string to clipboard
Y <- read.zoo('clipboard', tz="", sep=';')
merge(X,Y)
                     X      Y
 2016-08-09 23:59:52 24.600     NA
 2016-08-09 23:59:53 24.600     NA
 2016-08-09 23:59:54 24.600     NA
 2016-08-09 23:59:55 24.600 21.703
 2016-08-09 23:59:56 24.600 21.703
 2016-08-09 23:59:57 24.599 21.703
 2016-08-09 23:59:58     NA 21.703
 2016-08-09 23:59:59     NA 21.703
 2016-08-10 00:00:00     NA 21.703