Luke Collins - 5 months ago 26

R Question

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)

Answer

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:

`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.- 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.