Rearrange data frame in a list using dplyr in R

4

This is my dataframe:

dput(df)
structure(list(ind = structure(c(16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16440, 
16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440, 
16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440, 
16440, 16440), class = "Date"), date = structure(c(17810, 17831, 
17852, 17873, 17894, 17915, 17936, 17957, 17978, 17999, 18020, 
18041, 18062, 18083, 18104, 18125, 18146, 18167, 18188, 18209, 
18230, 18251, 18272, 18293, 18314, 18335, 18356, 18377, 18398, 
18419, 18440, 18461, 18482, 18503, 18524, 18545, 18566, 18587, 
18608, 18629, 18650, 18671, 18692, 18713, 18734, 18755, 18776, 
18797, 18818, 18839, 18860, 18881, 18902, 18923, 18944, 18965, 
18986, 19007, 19028, 19049, 19070, 19091, 19112, 19133, 19154, 
19175, 19196, 19217, 19238, 19259, 19280, 19301, 19322, 19343, 
19364, 19385, 19406, 19427, 19448, 19469, 19490, 19511, 19532, 
19553, 19574, 19595, 19616, 19637, 19658, 19679, 19700, 19721, 
19742, 19763, 19784, 19805, 19826, 19847, 19868, 19889, 19910, 
19931, 19952, 19973, 19994, 20015, 20036, 20057, 20078, 20099, 
20120, 20141, 20162, 20183, 20204, 20225, 20246, 20267, 20288, 
20309, 17810, 17831, 17852, 17873, 17894, 17915, 17936, 17957, 
17978, 17999, 18020, 18041, 18062, 18083, 18104, 18125, 18146, 
18167, 18188, 18209, 18230), class = "Date"), Betas_Model1 = c(11.3705103971604, 
0.815985668583493, 3.61812048719847, 11.3705103971604, 0.815985668583493, 
3.61812048719847, 11.3705103971604, 0.815985668583493, 3.61812048719847, 
11.3705103971604, 0.815985668583493, 3.61812048719847, 11.3705103971604, 
0.815985668583493, 3.61812048719847, 11.3705103971604, 0.815985668583493, 
3.61812048719847, 11.3705103971604, 0.815985668583493, 3.61812048719847, 
11.3705103971604, 0.815985668583493, 3.61812048719847, 11.3705103971604, 
0.815985668583493, 3.61812048719847, 11.3705103971604, 0.815985668583493, 
3.61812048719847, 11.3705103971604, 0.815985668583493, 3.61812048719847, 
11.3705103971604, 0.815985668583493, 3.61812048719847, 11.3705103971604, 
0.815985668583493, 3.61812048719847, 11.3705103971604, 0.815985668583493, 
3.61812048719847, 11.3705103971604, 0.815985668583493, 3.61812048719847, 
11.3705103971604, 0.815985668583493, 3.61812048719847, 11.3705103971604, 
0.815985668583493, 3.61812048719847, 11.3705103971604, 0.815985668583493, 
3.61812048719847, 11.3705103971604, 0.815985668583493, 3.61812048719847, 
11.3705103971604, 0.815985668583493, 3.61812048719847, 11.3705103971604, 
0.815985668583493, 3.61812048719847, 11.3705103971604, 0.815985668583493, 
3.61812048719847, 11.3705103971604, 0.815985668583493, 3.61812048719847, 
11.3705103971604, 0.815985668583493, 3.61812048719847, 11.3705103971604, 
0.815985668583493, 3.61812048719847, 11.3705103971604, 0.815985668583493, 
3.61812048719847, 11.3705103971604, 0.815985668583493, 3.61812048719847, 
11.3705103971604, 0.815985668583493, 3.61812048719847, 11.3705103971604, 
0.815985668583493, 3.61812048719847, 11.3705103971604, 0.815985668583493, 
3.61812048719847, 11.3705103971604, 0.815985668583493, 3.61812048719847, 
11.3705103971604, 0.815985668583493, 3.61812048719847, 11.3705103971604, 
0.815985668583493, 3.61812048719847, 11.3705103971604, 0.815985668583493, 
3.61812048719847, 11.3705103971604, 0.815985668583493, 3.61812048719847, 
11.3705103971604, 0.815985668583493, 3.61812048719847, 11.3705103971604, 
0.815985668583493, 3.61812048719847, 11.3705103971604, 0.815985668583493, 
3.61812048719847, 11.3705103971604, 0.815985668583493, 3.61812048719847, 
11.3705103971604, 0.815985668583493, 3.61812048719847, 11.4361888772212, 
0.785901410160401, 3.21321611089539, 11.4361888772212, 0.785901410160401, 
3.21321611089539, 11.4361888772212, 0.785901410160401, 3.21321611089539, 
11.4361888772212, 0.785901410160401, 3.21321611089539, 11.4361888772212, 
0.785901410160401, 3.21321611089539, 11.4361888772212, 0.785901410160401, 
3.21321611089539, 11.4361888772212, 0.785901410160401, 3.21321611089539
), Betas_Model2 = c(11.6356043415347, 0.267366795397781, 3.83700647995003, 
11.6356043415347, 0.267366795397781, 3.83700647995003, 11.6356043415347, 
0.267366795397781, 3.83700647995003, 11.6356043415347, 0.267366795397781, 
3.83700647995003, 11.6356043415347, 0.267366795397781, 3.83700647995003, 
11.6356043415347, 0.267366795397781, 3.83700647995003, 11.6356043415347, 
0.267366795397781, 3.83700647995003, 11.6356043415347, 0.267366795397781, 
3.83700647995003, 11.6356043415347, 0.267366795397781, 3.83700647995003, 
11.6356043415347, 0.267366795397781, 3.83700647995003, 11.6356043415347, 
0.267366795397781, 3.83700647995003, 11.6356043415347, 0.267366795397781, 
3.83700647995003, 11.6356043415347, 0.267366795397781, 3.83700647995003, 
11.6356043415347, 0.267366795397781, 3.83700647995003, 11.6356043415347, 
0.267366795397781, 3.83700647995003, 11.6356043415347, 0.267366795397781, 
3.83700647995003, 11.6356043415347, 0.267366795397781, 3.83700647995003, 
11.6356043415347, 0.267366795397781, 3.83700647995003, 11.6356043415347, 
0.267366795397781, 3.83700647995003, 11.6356043415347, 0.267366795397781, 
3.83700647995003, 11.6356043415347, 0.267366795397781, 3.83700647995003, 
11.6356043415347, 0.267366795397781, 3.83700647995003, 11.6356043415347, 
0.267366795397781, 3.83700647995003, 11.6356043415347, 0.267366795397781, 
3.83700647995003, 11.6356043415347, 0.267366795397781, 3.83700647995003, 
11.6356043415347, 0.267366795397781, 3.83700647995003, 11.6356043415347, 
0.267366795397781, 3.83700647995003, 11.6356043415347, 0.267366795397781, 
3.83700647995003, 11.6356043415347, 0.267366795397781, 3.83700647995003, 
11.6356043415347, 0.267366795397781, 3.83700647995003, 11.6356043415347, 
0.267366795397781, 3.83700647995003, 11.6356043415347, 0.267366795397781, 
3.83700647995003, 11.6356043415347, 0.267366795397781, 3.83700647995003, 
11.6356043415347, 0.267366795397781, 3.83700647995003, 11.6356043415347, 
0.267366795397781, 3.83700647995003, 11.6356043415347, 0.267366795397781, 
3.83700647995003, 11.6356043415347, 0.267366795397781, 3.83700647995003, 
11.6356043415347, 0.267366795397781, 3.83700647995003, 11.6356043415347, 
0.267366795397781, 3.83700647995003, 11.6356043415347, 0.267366795397781, 
3.83700647995003, 11.6616719281849, 0.286128773287116, 3.48472815397068, 
11.6616719281849, 0.286128773287116, 3.48472815397068, 11.6616719281849, 
0.286128773287116, 3.48472815397068, 11.6616719281849, 0.286128773287116, 
3.48472815397068, 11.6616719281849, 0.286128773287116, 3.48472815397068, 
11.6616719281849, 0.286128773287116, 3.48472815397068, 11.6616719281849, 
0.286128773287116, 3.48472815397068), Betas_Model3 = c(11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.7361984835266, 
8.5039084097167, -8.45218761189702, -1.88402205905606, 11.8656699782291, 
9.81495553149212, -9.92664234034938, -3.24875606367036, 11.8656699782291, 
9.81495553149212, -9.92664234034938, -3.24875606367036, 11.8656699782291, 
9.81495553149212, -9.92664234034938, -3.24875606367036, 11.8656699782291, 
9.81495553149212, -9.92664234034938, -3.24875606367036, 11.8656699782291, 
9.81495553149212, -9.92664234034938, -3.24875606367036, 11.8656699782291
), Betas_Model4 = c(11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.7592436585382, 0.0581495086393261, -1.49634135426921, 
5.31525910692628, 11.8955734783281, -0.109669657791056, -2.83078692756828, 
6.28129472626298, 11.8955734783281, -0.109669657791056, -2.83078692756828, 
6.28129472626298, 11.8955734783281, -0.109669657791056, -2.83078692756828, 
6.28129472626298, 11.8955734783281, -0.109669657791056, -2.83078692756828, 
6.28129472626298, 11.8955734783281, -0.109669657791056, -2.83078692756828, 
6.28129472626298, 11.8955734783281), Betas_Model5 = c(11.5746208819078, 
-100.07087058088, 100.240112647756, 17.6703189511514, 23.4507308143565, 
11.5746208819078, -100.07087058088, 100.240112647756, 17.6703189511514, 
23.4507308143565, 11.5746208819078, -100.07087058088, 100.240112647756, 
17.6703189511514, 23.4507308143565, 11.5746208819078, -100.07087058088, 
100.240112647756, 17.6703189511514, 23.4507308143565, 11.5746208819078, 
-100.07087058088, 100.240112647756, 17.6703189511514, 23.4507308143565, 
11.5746208819078, -100.07087058088, 100.240112647756, 17.6703189511514, 
23.4507308143565, 11.5746208819078, -100.07087058088, 100.240112647756, 
17.6703189511514, 23.4507308143565, 11.5746208819078, -100.07087058088, 
100.240112647756, 17.6703189511514, 23.4507308143565, 11.5746208819078, 
-100.07087058088, 100.240112647756, 17.6703189511514, 23.4507308143565, 
11.5746208819078, -100.07087058088, 100.240112647756, 17.6703189511514, 
23.4507308143565, 11.5746208819078, -100.07087058088, 100.240112647756, 
17.6703189511514, 23.4507308143565, 11.5746208819078, -100.07087058088, 
100.240112647756, 17.6703189511514, 23.4507308143565, 11.5746208819078, 
-100.07087058088, 100.240112647756, 17.6703189511514, 23.4507308143565, 
11.5746208819078, -100.07087058088, 100.240112647756, 17.6703189511514, 
23.4507308143565, 11.5746208819078, -100.07087058088, 100.240112647756, 
17.6703189511514, 23.4507308143565, 11.5746208819078, -100.07087058088, 
100.240112647756, 17.6703189511514, 23.4507308143565, 11.5746208819078, 
-100.07087058088, 100.240112647756, 17.6703189511514, 23.4507308143565, 
11.5746208819078, -100.07087058088, 100.240112647756, 17.6703189511514, 
23.4507308143565, 11.5746208819078, -100.07087058088, 100.240112647756, 
17.6703189511514, 23.4507308143565, 11.5746208819078, -100.07087058088, 
100.240112647756, 17.6703189511514, 23.4507308143565, 11.5746208819078, 
-100.07087058088, 100.240112647756, 17.6703189511514, 23.4507308143565, 
11.5746208819078, -100.07087058088, 100.240112647756, 17.6703189511514, 
23.4507308143565, 11.5746208819078, -100.07087058088, 100.240112647756, 
17.6703189511514, 23.4507308143565, 11.5746208819078, -100.07087058088, 
100.240112647756, 17.6703189511514, 23.4507308143565, 11.7649976109683, 
-70.9266830550256, 70.8955844566333, 10.7249798063487, 19.1077454567509, 
11.7649976109683, -70.9266830550256, 70.8955844566333, 10.7249798063487, 
19.1077454567509, 11.7649976109683, -70.9266830550256, 70.8955844566333, 
10.7249798063487, 19.1077454567509, 11.7649976109683, -70.9266830550256, 
70.8955844566333, 10.7249798063487, 19.1077454567509, 11.7649976109683
)), row.names = c(NA, -141L), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), vars = "ind", drop = TRUE, indices = list(
    0:119, 120:140), group_sizes = c(120L, 21L), biggest_group_size = 120L, labels = structure(list(
    ind = structure(c(16437, 16440), class = "Date")), row.names = c(NA, 
-2L), class = "data.frame", vars = "ind", drop = TRUE))

As you can see, there are columns with betas that were estimated from each model. are 5 models. Each date in the ind column has its betas.

For example: the date "2015-01-02" has its betas. Notice that the number of betas of model one equals three. In the model 1 betas column these 3 betas repeat. This happens for all betas columns. In the model5 column are 5 betas related to this model, therefore, these betas repeat every 5 times. Same thing for the next date of the ind. Column.

My idea is to create a list, preferably with the dplyr package to organize those betas. Thus, each entry in that list would come with entries for the templates being filled by the betas of each template, as below, for example:

  betalist
[[2015-01-02]] #primeira data da coluna ind

       [Betas_Model1] 
       11.4 0.816 3.62
       [Betas_Model2] 
       11.6  0.267 3.8
       [Betas_Model3] 
      11.7 8.50 -8.45 -1.88
       [Betas_Model4] 
      11.8 0.0581 -1.50 5.32
       [Betas_Model5] 
      11.6 -100. 100. 17.7 23.5
 [[2015-01-05]] #segunda data da coluna ind
.
.
.
.
.
.

Some personal help?

    
asked by anonymous 16.09.2018 / 22:29

1 answer

5

I do not know if this is what you want, but the result res has a format similar to that of the question.

sp <- split(df[3:7], df$ind)
res <- lapply(sp, function(s) lapply(s, unique))

str(res)
#List of 2
# $ 2015-01-02:List of 5
#  ..$ Betas_Model1: num [1:3] 11.371 0.816 3.618
#  ..$ Betas_Model2: num [1:3] 11.636 0.267 3.837
#  ..$ Betas_Model3: num [1:4] 11.74 8.5 -8.45 -1.88
#  ..$ Betas_Model4: num [1:4] 11.7592 0.0581 -1.4963 5.3153
#  ..$ Betas_Model5: num [1:5] 11.6 -100.1 100.2 17.7 23.5
# $ 2015-01-05:List of 5
#  ..$ Betas_Model1: num [1:3] 11.436 0.786 3.213
#  ..$ Betas_Model2: num [1:3] 11.662 0.286 3.485
#  ..$ Betas_Model3: num [1:4] 11.87 9.81 -9.93 -3.25
#  ..$ Betas_Model4: num [1:4] 11.9 -0.11 -2.83 6.28
#  ..$ Betas_Model5: num [1:5] 11.8 -70.9 70.9 10.7 19.1
    
17.09.2018 / 07:00