How do you put three time series into one?

Asked

Viewed 261 times

2

I’m trying to put three time series together in one, but I’m having a lot of problems.

I used the junction c of R, but it is not useful to me because as it comes to monthly series, this command mixes the observations of the three series and becomes a mess.

I used the package plyr and the skinny to create a data.frame containing the three previous series as columns, but with the first package, the function join_all returned me a data.frame empty, and with the second R says that the series parameters are invalid (ps.: but they contain the same dimension).

Below I leave the commands I used for a better understanding:

cimento <- read.table(file='cimento.csv', sep=';', header=T)

Carbono01 <- (300 + (0.2*cimento))
Carbono02 <- (300 + (0.2*(cimento^2/2)))
Carbono03 <- (300 + (0.2*cimento) + (cimento^2/4000))

Carbono01 <- as.data.frame(Carbono01)
Carbono02 <- as.data.frame(Carbono02)
Carbono03 <- as.data.frame(Carbono03)

install.packages("plyr")
library(plyr) 
carbono <- join_all(list(Carbono01, Carbono02,Carbono03)) 
carbono
rm(carbono)

install.packages("magrittr")
library(magrittr)
carbono <- Carbono01%>%merge(Carbono02)%>%merge(Carbono03)
carbono

Edit: str(cimento)

Time-Series [1:242] from 1996 to 2016: 3391 3436 3630 3677 3460 3308 3645 3707 3677 3629 ...
  • Welcome to Stackoverflow! Unfortunately, this question cannot be reproduced by anyone trying to answer it. Please, take a look at this link and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.

  • updates the question with the result of str(cimento)

  • Done...........

  • Try Carbono01 <- data.frame(ID = seq_along(Carbono01), Carbono01) and the same for others. Without a common column (in this case ID) how does the join_all can join df’s? And still, for time series, see the packages zoo or xts.

  • Good afternoon, buddy! I’ve been trying to do what you recommended, but the R only returns this as an answer: Error in [<-.ts(tmp, ri, value = c(1150188.1, 1180909.6, 1317990, : &#xA; only replacement of elements is allowed I don’t understand what’s happening.

1 answer

2

I don’t know how exactly your series is in the file cimento.csv. The ideal would be for you to provide a time series exactly as yours is. However I believe that the problem to join time series can be solved transparently using the package xts. Take an example:

library(xts)
serie1 <-as.xts(ts(c(1:12),star=c(2014,1),freq=12))
serie2 <-as.xts(ts(c(13:24),star=c(2015,1),freq=12))
serie3 <- c(serie1, serie2))

Following each series as it turned out:

> serie1
         [,1]
jan 2014    1
fev 2014    2
mar 2014    3
abr 2014    4
mai 2014    5
jun 2014    6
jul 2014    7
ago 2014    8
set 2014    9
out 2014   10
nov 2014   11
dez 2014   12

> serie2
         [,1]
jan 2015   13
fev 2015   14
mar 2015   15
abr 2015   16
mai 2015   17
jun 2015   18
jul 2015   19
ago 2015   20
set 2015   21
out 2015   22
nov 2015   23
dez 2015   24

> serie3
         [,1]
jan 2014    1
fev 2014    2
mar 2014    3
abr 2014    4
mai 2014    5
jun 2014    6
jul 2014    7
ago 2014    8
set 2014    9
out 2014   10
nov 2014   11
dez 2014   12
jan 2015   13
fev 2015   14
mar 2015   15
abr 2015   16
mai 2015   17
jun 2015   18
jul 2015   19
ago 2015   20
set 2015   21
out 2015   22
nov 2015   23
dez 2015   24

the interesting thing is that the method c() when it comes to objects xts already does all the dirty work for you.

  • Hello, Flávio! Solved, but not separated by columns, as was my intention. But thank you very much, because at least you put it all together in one object, something you couldn’t possibly achieve.

  • @Leonardovinicius if satisfied with the answer mark as the choice (using the tick) ;-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.