使用dplyr重新组织列表中具有不同长度元素的数据框

时间:2018-09-16 19:47:23

标签: r dplyr

这是我的df

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))

对于ind列中的每个日期,我都有在回归模型中估算的Beta。这些测试版位于Betas_ModelX列上。

例如,关于日期“ 2015-01-02”,我有每种型号的beta。

它们是:

Model1 - > 11.4 0.816 3.62; Model2 -> 11.6 0.267 3.8; Model3-> 11.7 8.50 -8.45 -1.88; Model4 -> 11.8 0.0581 -1.50 5.32; Model5->11.6 -100. 100. 17.7 23.5

如您所见,对于ind列中的每个日期,我都有beta,更重要的是,这些beta的数量是不同的。例如,对于第一个模型,我有3个beta,对于第5个模型,我有5个beta。

我面临的挑战是使用dplyr包中的某些功能找到一种重新组织它的方法。有可能吗?

最终结果应该是每个ind日期及其beta的列表,如下所示(列表的结构由我完成):

  betalist
[[2015-01-02]]

       [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]]
.
.
.
.
.
.

主要挑战是如何找到一种方法,将ind列中的每个日期与其自己的Beta相关联。

有帮助吗?

1 个答案:

答案 0 :(得分:1)

我有一个不使用dplyr但使用lapply的解决方案。 plouf是您的数据:

liste <- lapply(unique(plouf$ind),function(y){
  tmp <- plouf[plouf$ind == y,]
  sapply(grep("Betas_Model",names(plouf),value = T),function(x){
    unique(tmp[[x]])
  })
})

names(liste) <- unique(plouf$ind)
liste


$`2015-01-02`
$`2015-01-02`$Betas_Model1
[1] 11.3705104  0.8159857  3.6181205

$`2015-01-02`$Betas_Model2
[1] 11.6356043  0.2673668  3.8370065

$`2015-01-02`$Betas_Model3
[1] 11.736198  8.503908 -8.452188 -1.884022

$`2015-01-02`$Betas_Model4
[1] 11.75924366  0.05814951 -1.49634135  5.31525911

$`2015-01-02`$Betas_Model5
[1]   11.57462 -100.07087  100.24011   17.67032   23.45073


$`2015-01-05`
$`2015-01-05`$Betas_Model1
[1] 11.4361889  0.7859014  3.2132161

$`2015-01-05`$Betas_Model2
[1] 11.6616719  0.2861288  3.4847282

$`2015-01-05`$Betas_Model3
[1] 11.865670  9.814956 -9.926642 -3.248756

$`2015-01-05`$Betas_Model4
[1] 11.8955735 -0.1096697 -2.8307869  6.2812947

$`2015-01-05`$Betas_Model5
[1]  11.76500 -70.92668  70.89558  10.72498  19.10775

从表面上看,您只需要具有每个列的唯一值,因此所需的主要功能是unique(),并且要获得结果列表时,将使用lapply两次。在第一个循环中,我用感兴趣的日期对数据进行了子集

tmp <- plouf[plouf$ind == y,]

,然后在列(grep("Betas_Model",names(plouf),value = T))上循环以获取此子集中的唯一值

unique(tmp[[x]])

希望它能完成工作