Problems to transform variable data timeSeries in R

Asked

Viewed 33 times

0

I have the following data and would like to create 4 dataframe:

> head(Log_retorno)
             TS.1.1      TS.1.2       TS.1.3      TS.1.4
2008-01-11  0.090393077  0.08588355  0.129032018  0.09774478
2008-01-18  0.017924523  0.02176237  0.014430686  0.02192171
2008-01-25 -0.021960031 -0.04316860 -0.034300853 -0.04978512
2008-02-01  0.037062020  0.05003171  0.015529532  0.04156171
2008-02-08  0.052798186  0.02240354  0.041780025  0.04465233
2008-02-15 -0.002929457  0.02495560  0.007350643  0.01031265
> dim(Log_retorno)
[1] 572   4
> str(Log_retorno)
'data.frame':   572 obs. of  4 variables:
$ TS.1.1: num  0.0904 0.0179 -0.022 0.0371 0.0528 ...
$ TS.1.2: num  0.0859 0.0218 -0.0432 0.05 0.0224 ...
$ TS.1.3: num  0.129 0.0144 -0.0343 0.0155 0.0418 ...
$ TS.1.4: num  0.0977 0.0219 -0.0498 0.0416 0.0447 ...

How do I separate variables and put the date into each dataframe? Ex:

   Data       TS.1.1     
2008-01-11  0.090393077  
2008-01-18  0.017924523  
2008-01-25 -0.021960031 
2008-02-01  0.037062020  
2008-02-08  0.052798186  
2008-02-15 -0.002929457  
     Data     TS.1.2       
2008-01-11  0.08588355  
2008-01-18  0.02176237  
2008-01-25 -0.04316860 
2008-02-01  0.05003171  
2008-02-08  0.02240354  
2008-02-15  0.02495560   

1 answer

2


Try the following function. Keep the line names.

ls()
#[1] "Log_retorno"

splitByColumn <- function(DF, envir = .GlobalEnv){
  r <- lapply(names(DF), function(x) {
    assign(x, DF[, x, drop = FALSE], envir = envir)
  })
  invisible(r)
}

splitByColumn(Log_retorno)

ls()
#[1] "Log_retorno"   "splitByColumn" "TS.1.1"       
#[4] "TS.1.2"        "TS.1.3"        "TS.1.4"       

TS.1.1
#                 TS.1.1
#2008-01-11  0.090393077
#2008-01-18  0.017924523
#2008-01-25 -0.021960031
#2008-02-01  0.037062020
#2008-02-08  0.052798186
#2008-02-15 -0.002929457

Dice.

Log_retorno <- read.table(text = "
             TS.1.1      TS.1.2       TS.1.3      TS.1.4
2008-01-11  0.090393077  0.08588355  0.129032018  0.09774478
2008-01-18  0.017924523  0.02176237  0.014430686  0.02192171
2008-01-25 -0.021960031 -0.04316860 -0.034300853 -0.04978512
2008-02-01  0.037062020  0.05003171  0.015529532  0.04156171
2008-02-08  0.052798186  0.02240354  0.041780025  0.04465233
2008-02-15 -0.002929457  0.02495560  0.007350643  0.01031265
", header = TRUE)

Browser other questions tagged

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