-2

I have a Time Series data in the form of a list, which has been defined as below

[[1]][[1]] [1] "Product Name"

[[1]][[2]] [1] 20031031 "Starting duration"

[[1]][[3]] [1] 20160831 "Ending Duration"

[[1]][[4]] [1] 0.040816323 0.010141988 0.050318689 0.012804102 0.011378003 "Series of observations"

I for instance try to merge two series using: merge(as.zoo(TS1), as.zoo(TS2))

The functionality works fine when its not inside a loop, and is distorted while being used inside a for loop

startYear = as.numeric(substr(unlist(TimeSeries[[1]][2]),1,4))

startMonth = as.numeric(substr(unlist(TimeSeries[[1]][2]),5,6))

TS1 = ts(unlist(TimeSeries[[1]][4]), start = c(startYear,startMonth), 
frequency = 12)

TS2 = ts(unlist(TimeSeries[[2]][4]), start = c(startYear,startMonth), 
frequency = 12)

M <- merge(as.zoo(TS1), as.zoo(TS2))

No issues seen here in the above merge

issues seen when same code is used inside a For loop,

for(I in 1:1)
{
M <- merge(as.zoo(TS1), as.zoo(TS2))
}

Why is this happening, and what could possibly be a fix for same

Anoop Js
  • 39
  • 8
  • 1
    You need to show us your code – Phantom Photon Feb 02 '17 at 17:44
  • Post your code so that any of us can recreate the problem, and find a solution, on our own. Try dput(), or check out this thread: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jesstme Feb 02 '17 at 18:05
  • @Edward, I have updated and edited my question, please let me know if you need more details – Anoop Js Feb 02 '17 at 18:15

1 Answers1

0

I think merge is working just fine.

Given the following code:

library("zoo")

a = as.zoo(ts(rnorm(5), start = 1981, freq = 12))

b = as.zoo(ts(rnorm(10), start = 1981, freq = 12))

merge(a,b)

for(I in 1:1){
    merge(a,b)
}

only the first merge(a,b) will print to the console-- in order to print to the console in a for loop you need to call print explicitly.

You can see this by executing:

for (I in 1:1){
    1:10
}

and

for (I in 1:1){
    print(1:10)
}

This does not mean that the code inside the loop will not be executed!

See the output of the following code:

library("zoo")

a = as.zoo(ts(rnorm(5), start = 1981, freq = 12))

b = as.zoo(ts(rnorm(10), start = 1981, freq = 12))

new1 <- merge(a,b)

for(I in 1:1){
    new2 <- merge(a,b)
}

new1
new2

Output:

                  a          b
1981(1)   0.2137495 -0.4148458
1981(2)  -0.1298899 -0.4880588
1981(3)   0.2452036  0.1619693
1981(4)  -0.2251848  0.4866681
1981(5)   1.1049321  1.2171081
1981(6)          NA -0.5852703
1981(7)          NA  1.5238886
1981(8)          NA -0.7583958
1981(9)          NA -0.2972132
1981(10)         NA -0.4476471
                  a          b
1981(1)   0.2137495 -0.4148458
1981(2)  -0.1298899 -0.4880588
1981(3)   0.2452036  0.1619693
1981(4)  -0.2251848  0.4866681
1981(5)   1.1049321  1.2171081
1981(6)          NA -0.5852703
1981(7)          NA  1.5238886
1981(8)          NA -0.7583958
1981(9)          NA -0.2972132
1981(10)         NA -0.4476471
Phantom Photon
  • 768
  • 2
  • 10
  • 20