处理数据帧列表的列表

时间:2012-06-07 15:16:57

标签: r

Losses
[[1]]
[[1]]$`1`
   YearID Category       Loss
1       2    US HU  826.79527
2       4    US HU  357.37926
3       5    US HU  148.71542
4       5    US HU  654.44473
5       8    US HU   81.74428
6       8    US HU   60.27192
7       8    US HU   63.59582
8      10    US HU  778.97650
9      10    US HU 1498.90801
10     11    US HU   66.72708
11     11    US HU  123.25430
12     11    US HU   70.87750
13     11    US HU  272.57985
14     12    US HU   77.14205
15     13    US HU  305.00484
16     16    US HU  577.17381
17     16    US HU  137.12441
18     20    US HU   53.29270
19     20    US HU  123.86174

[[1]]$`2`
   YearID Category        Loss
1       2    US EQ   2.6895238
2       9    US EQ  42.7690970
3       9    US EQ  10.7816904
4      11    US EQ   2.3136544
5      12    US EQ   0.4897316
6      13    US EQ   1.0560520
7      15    US EQ 111.0338641
8      17    US EQ 123.7145577
9      19    US EQ   8.1518931
10     19    US EQ   0.3099158

[[1]]$`3`
  YearID Category       Loss
1      2    JP EQ  0.2920082
2      5    JP EQ 64.6424343
3      7    JP EQ  0.6097201
4      7    JP EQ  6.7342614
5     10    JP EQ  2.9948139
6     10    JP EQ 12.8309552
7     15    JP EQ  0.2373154
8     16    JP EQ  1.6869774
9     20    JP EQ  1.3283421


[[2]]
[[2]]$`1`
   YearID Category       Loss
1      15    US HU  107.63285
2      16    US HU  192.89825
3      19    US HU  215.81496
4       2    US HU  826.79527
5       4    US HU  357.37926
6       5    US HU  148.71542
7       5    US HU  654.44473
8       8    US HU   81.74428
9       8    US HU   60.27192
10      8    US HU   63.59582
11     10    US HU  778.97650
12     10    US HU 1498.90801
13     11    US HU   66.72708
14     11    US HU  123.25430
15     11    US HU   70.87750
16     11    US HU  272.57985
17     12    US HU   77.14205
18     13    US HU  305.00484
19     16    US HU  577.17381
20     16    US HU  137.12441
21     20    US HU   53.29270
22     20    US HU  123.86174

[[2]]$`2`
   YearID Category        Loss
1       5    US EQ   0.6082396
2      10    US EQ 159.8625057
3       2    US EQ   2.6895238
4       9    US EQ  42.7690970
5       9    US EQ  10.7816904
6      11    US EQ   2.3136544
7      12    US EQ   0.4897316
8      13    US EQ   1.0560520
9      15    US EQ 111.0338641
10     17    US EQ 123.7145577
11     19    US EQ   8.1518931
12     19    US EQ   0.3099158

[[2]]$`3`
   YearID Category       Loss
1       7    JP EQ  0.3618008
2      12    JP EQ 11.0977981
3       2    JP EQ  0.2920082
4       5    JP EQ 64.6424343
5       7    JP EQ  0.6097201
6       7    JP EQ  6.7342614
7      10    JP EQ  2.9948139
8      10    JP EQ 12.8309552
9      15    JP EQ  0.2373154
10     16    JP EQ  1.6869774
11     20    JP EQ  1.3283421


[[3]]
[[3]]$`1`
   YearID Category       Loss
1       4    US HU   72.52669
2       8    US HU  270.71636
3      10    US HU  132.25629
4      10    US HU   49.67546
5      11    US HU  134.83450
6      12    US HU  116.10639
7      18    US HU  409.75564
8      19    US HU  268.86671
9      20    US HU  576.44159
10     15    US HU  107.63285
11     16    US HU  192.89825
12     19    US HU  215.81496
13      2    US HU  826.79527
14      4    US HU  357.37926
15      5    US HU  148.71542
16      5    US HU  654.44473
17      8    US HU   81.74428
18      8    US HU   60.27192
19      8    US HU   63.59582
20     10    US HU  778.97650
21     10    US HU 1498.90801
22     11    US HU   66.72708
23     11    US HU  123.25430
24     11    US HU   70.87750
25     11    US HU  272.57985
26     12    US HU   77.14205
27     13    US HU  305.00484
28     16    US HU  577.17381
29     16    US HU  137.12441
30     20    US HU   53.29270
31     20    US HU  123.86174

[[3]]$`2`
   YearID Category        Loss
1       6    US EQ   3.6683023
2      10    US EQ   4.6943200
3      11    US EQ   2.6239743
4      13    US EQ   1.3989827
5      13    US EQ   0.2399834
6      19    US EQ   5.8900987
7       5    US EQ   0.6082396
8      10    US EQ 159.8625057
9       2    US EQ   2.6895238
10      9    US EQ  42.7690970
11      9    US EQ  10.7816904
12     11    US EQ   2.3136544
13     12    US EQ   0.4897316
14     13    US EQ   1.0560520
15     15    US EQ 111.0338641
16     17    US EQ 123.7145577
17     19    US EQ   8.1518931
18     19    US EQ   0.3099158

[[3]]$`3`
   YearID Category        Loss
1       1    JP EQ   0.9199341
2       2    JP EQ   2.7294618
3       7    JP EQ 224.4266519
4      11    JP EQ   0.3948219
5      14    JP EQ   0.7592534
6      15    JP EQ   1.2838320
7       7    JP EQ   0.3618008
8      12    JP EQ  11.0977981
9       2    JP EQ   0.2920082
10      5    JP EQ  64.6424343
11      7    JP EQ   0.6097201
12      7    JP EQ   6.7342614
13     10    JP EQ   2.9948139
14     10    JP EQ  12.8309552
15     15    JP EQ   0.2373154
16     16    JP EQ   1.6869774
17     20    JP EQ   1.3283421

-

list(structure(list(`1` = structure(list(YearID = c(2, 4, 5, 
5, 8, 8, 8, 10, 10, 11, 11, 11, 11, 12, 13, 16, 16, 20, 20), 
    Category = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), class = "factor", .Label = "US HU"), 
    Loss = c(826.795273886473, 357.379257145211, 148.715422723442, 
    654.444727907641, 81.7442792092622, 60.2719188870034, 63.5958204662617, 
    778.976495232166, 1498.90801288032, 66.7270779301846, 123.254304652346, 
    70.8774963289464, 272.579854338023, 77.1420493089197, 305.004836528708, 
    577.173809957773, 137.124412010961, 53.2926961871035, 123.861739643341
    )), .Names = c("YearID", "Category", "Loss"), row.names = c(NA, 
-19L), class = "data.frame"), `2` = structure(list(YearID = c(2, 
9, 9, 11, 12, 13, 15, 17, 19, 19), Category = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), class = "factor", .Label = "US EQ"), 
    Loss = c(2.68952379748490, 42.7690970302631, 10.7816904147645, 
    2.31365444951509, 0.489731609222403, 1.05605200325909, 111.033864050878, 
    123.714557672790, 8.15189312989583, 0.309915764784634)), .Names = c("YearID", 
"Category", "Loss"), row.names = c(NA, -10L), class = "data.frame"), 
    `3` = structure(list(YearID = c(2, 5, 7, 7, 10, 10, 15, 16, 
    20), Category = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L), class = "factor", .Label = "JP EQ"), Loss = c(0.292008160090971, 
    64.6424342762608, 0.609720084708476, 6.73426138743874, 2.99481387294425, 
    12.8309551549701, 0.237315428236897, 1.68697744368254, 1.32834211322205
    )), .Names = c("YearID", "Category", "Loss"), row.names = c(NA, 
    -9L), class = "data.frame")), .Names = c("1", "2", "3")), 
    structure(list(`1` = structure(list(YearID = c(15, 16, 19, 
    2, 4, 5, 5, 8, 8, 8, 10, 10, 11, 11, 11, 11, 12, 13, 16, 
    16, 20, 20), Category = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L), .Label = "US HU", class = "factor"), Loss = c(107.632845241511, 
    192.898254968436, 215.814957575895, 826.795273886473, 357.379257145211, 
    148.715422723442, 654.444727907641, 81.7442792092622, 60.2719188870034, 
    63.5958204662617, 778.976495232166, 1498.90801288032, 66.7270779301846, 
    123.254304652346, 70.8774963289464, 272.579854338023, 77.1420493089197, 
    305.004836528708, 577.173809957773, 137.124412010961, 53.2926961871035, 
    123.861739643341)), .Names = c("YearID", "Category", "Loss"
    ), row.names = c(NA, 22L), class = "data.frame"), `2` = structure(list(
        YearID = c(5, 10, 2, 9, 9, 11, 12, 13, 15, 17, 19, 19
        ), Category = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L), .Label = "US EQ", class = "factor"), 
        Loss = c(0.60823959378077, 159.862505662270, 2.68952379748490, 
        42.7690970302631, 10.7816904147645, 2.31365444951509, 
        0.489731609222403, 1.05605200325909, 111.033864050878, 
        123.714557672790, 8.15189312989583, 0.309915764784634
        )), .Names = c("YearID", "Category", "Loss"), row.names = c(NA, 
    12L), class = "data.frame"), `3` = structure(list(YearID = c(7, 
    12, 2, 5, 7, 7, 10, 10, 15, 16, 20), Category = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "JP EQ", class = "factor"), 
        Loss = c(0.36180083325003, 11.0977980698104, 0.292008160090971, 
        64.6424342762608, 0.609720084708476, 6.73426138743874, 
        2.99481387294425, 12.8309551549701, 0.237315428236897, 
        1.68697744368254, 1.32834211322205)), .Names = c("YearID", 
    "Category", "Loss"), row.names = c(NA, 11L), class = "data.frame")), .Names = c("1", 
    "2", "3")), structure(list(`1` = structure(list(YearID = c(4, 
    8, 10, 10, 11, 12, 18, 19, 20, 15, 16, 19, 2, 4, 5, 5, 8, 
    8, 8, 10, 10, 11, 11, 11, 11, 12, 13, 16, 16, 20, 20), Category = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
    ), .Label = "US HU", class = "factor"), Loss = c(72.5266949028921, 
    270.716360695826, 132.256285323478, 49.6754599765934, 134.834496124065, 
    116.106394912601, 409.755636985602, 268.866707090942, 576.441585784339, 
    107.632845241511, 192.898254968436, 215.814957575895, 826.795273886473, 
    357.379257145211, 148.715422723442, 654.444727907641, 81.7442792092622, 
    60.2719188870034, 63.5958204662617, 778.976495232166, 1498.90801288032, 
    66.7270779301846, 123.254304652346, 70.8774963289464, 272.579854338023, 
    77.1420493089197, 305.004836528708, 577.173809957773, 137.124412010961, 
    53.2926961871035, 123.861739643341)), .Names = c("YearID", 
    "Category", "Loss"), row.names = c(NA, 31L), class = "data.frame"), 
        `2` = structure(list(YearID = c(6, 10, 11, 13, 13, 19, 
        5, 10, 2, 9, 9, 11, 12, 13, 15, 17, 19, 19), Category = structure(c(1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L), .Label = "US EQ", class = "factor"), Loss = c(3.66830233362805, 
        4.69432002882382, 2.62397430771946, 1.39898268940725, 
        0.239983432670597, 5.89009873171407, 0.60823959378077, 
        159.862505662270, 2.68952379748490, 42.7690970302631, 
        10.7816904147645, 2.31365444951509, 0.489731609222403, 
        1.05605200325909, 111.033864050878, 123.714557672790, 
        8.15189312989583, 0.309915764784634)), .Names = c("YearID", 
        "Category", "Loss"), row.names = c(NA, 18L), class = "data.frame"), 
        `3` = structure(list(YearID = c(1, 2, 7, 11, 14, 15, 
        7, 12, 2, 5, 7, 7, 10, 10, 15, 16, 20), Category = structure(c(1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L), .Label = "JP EQ", class = "factor"), Loss = c(0.91993405093065, 
        2.72946175093358, 224.426651937984, 0.394821850501270, 
        0.75925338029943, 1.28383204511935, 0.36180083325003, 
        11.0977980698104, 0.292008160090971, 64.6424342762608, 
        0.609720084708476, 6.73426138743874, 2.99481387294425, 
        12.8309551549701, 0.237315428236897, 1.68697744368254, 
        1.32834211322205)), .Names = c("YearID", "Category", 
        "Loss"), row.names = c(NA, 17L), class = "data.frame")), .Names = c("1", 
    "2", "3")))

-

InputList = list(c(1.5, 1.2), c(0.9, 0.8), c(1, 1))

InputList的长度始终与Losses的长度相同。 即会有相同的没有。 InputList中的向量作为Losses的长度。

我必须首先根据向量的第一个元素按递增顺序排列InputList 即。

InputList = list(c(0.9, 0.8), c(1, 1),c(1.5, 1.2))

现在,我必须将InputList的第一个向量的第二个元素相乘,即这里它与第一个损失列表中所有数据帧的损失列相加,即损失[[1]]

并且类似地将1与损失[[2]]

中所有数据帧的Loss列相乘

我希望将结果作为类似于原始列表的列表丢失,只是每个数据框的损失列都相应地膨胀或收缩。

1 个答案:

答案 0 :(得分:2)

Q1:“我必须首先根据向量的第一个元素按递增顺序排列InputList,即

 newInput <- InputList[order(sapply(InputList, "[[", 1))]
 newInput
[[1]]
[1] 0.9 0.8

[[2]]
[1] 1 1

[[3]]
[1] 1.5 1.2

Q2:“现在,我必须将InputList的第一个向量的第二个元素相乘,即在这里它与第一个损失列表中的所有数据帧的损失列为0.8,即损失[[1]],并且类似将1与损失[[2]]“

中所有数据帧的Loss列相乘

(我认为这意味着损失[[1]]中的所有三个数据帧都需要将其损失列乘以0.9。)

这将返回一个3 x 3的数据帧矩阵,其损耗列乘以所需的因子:

ddapply <- function(dfrmlist,fac){ 
         lapply(dfrmlist, 
             function(dfrm){ dfrm[, "Loss"] <- dfrm[ ,"Loss"]*fac; return(dfrm)})}
mapply( ddapply,  Losses, sapply(newInput, "[[", 2) )

第一组数据帧值位于第一列,而不是第一行,因为R矩阵中元素的列主要排序。所以这是损失的第一个元素中的第二个data.frame:

> mapply( ddapply,  Losses, sapply(newInput, "[[", 2) )[2,1]

$`2`
   YearID Category       Loss
1       2    US EQ  2.1516190
2       9    US EQ 34.2152776
3       9    US EQ  8.6253523
4      11    US EQ  1.8509236
5      12    US EQ  0.3917853
6      13    US EQ  0.8448416
7      15    US EQ 88.8270912
8      17    US EQ 98.9716461
9      19    US EQ  6.5215145
10     19    US EQ  0.2479326