-1

I have around thirty separate time series in R. I would like to put them all inside one large data set but can not seem to do this.

I have used the following code but it doesn't work. All my time series are names ts1,ts2 etc. if i was to do df <- data.frame(ts1,ts2) this works individually but not if I input it this way

for(i in 2:nrow(deal)) 
{ 
temp <- paste("ts",i,sep="")
mystring <- paste(mystring,temp,sep=",") 
} 
df <- data.frame(mystring)
Luke Collins
  • 39
  • 1
  • 5
  • Use the [edit](http://stackoverflow.com/posts/38715951/edit) to update your post, with further info and code. – zx8754 Aug 02 '16 at 08:59

1 Answers1

0

Given that df <- data.frame(ts1,ts2) works, the following should work:

N <- nrow(deal)   # or whatever number of time series you have
df <- data.frame(sapply(1:N, function(i) eval(parse(text=paste("ts",i,sep="")))))

Notes:

  1. sapply loops over sequence from 1 to N and applies a function. The results of the function are gathered as columns to a matrix that is then coerced into a data frame.
  2. The function that is applied constructs the string for the name of the i-th time series and uses this SO answer to evaluate the expression from the string. This returns the time series.

Hope this helps.

Community
  • 1
  • 1
aichao
  • 7,375
  • 3
  • 16
  • 18