dimnames和sapply - 如何在.GlobalEnv中改变价值

时间:2016-02-03 08:34:35

标签: r

我有一个愚蠢的问题。例如,我有这样的代码:

resultNorm <- lapply(2:5, function(numBeans) { parSapply(cl, 1:iterations, GetBinsNormDistribs, numBeans = numBeans) });

所以,我的列表包含这样的数据:

> resultNorm 
[[1]]
          [,1]       [,2]      [,3]
[1,] 0.2246621 0.09398241 0.1356241
[2,] 0.3107575 0.05482151 0.1331455
[3,] 0.9261597 0.77957860 0.8280028
[4,] 1.2077192 1.17804615 1.0985935

[[2]]
           [,1]       [,2]       [,3]
[1,] 0.04151756 0.07737258 0.06166794
[2,] 0.04680101 0.07138281 0.07005758
[3,] 0.98120583 0.96825317 0.96955009
[4,] 1.52177642 1.57192003 1.62818979

[[3]]
          [,1]      [,2]       [,3]
[1,] 0.1151765 0.1733337 0.07806343
[2,] 0.1405760 0.2070153 0.07304057
[3,] 0.8228107 0.9457689 1.03273787
[4,] 1.6026743 1.7581600 1.89761473

[[4]]
          [,1]      [,2]       [,3]
[1,] 0.1360939 0.1590460 0.08682901
[2,] 0.1520694 0.2047105 0.07762287
[3,] 0.9121020 1.0151550 0.96390948
[4,] 1.9088521 2.0688248 1.99595126

所以,我想在此列表中使用dimnames并为每一行指定唯一名称: 我尝试了这样的代码,但没有任何结果:

devNull <- sapply(1:length(resultNorm), function(i, envir = .GlobalEnv) { dimnames(resultNorm[[i]]) <- list(c("a1", "a2", "a3", "a4")) })

resultNorm没有任何变化...... 您能否告诉我,如何使用sapply.GlobalEnv数据?

我想获得这样的输出:

> resultNorm 
[[1]]
          [,1]       [,2]      [,3]
a1 0.2246621 0.09398241 0.1356241
a2 0.3107575 0.05482151 0.1331455
a3 0.9261597 0.77957860 0.8280028
a4 1.2077192 1.17804615 1.0985935

[[2]]
           [,1]       [,2]       [,3]
a1 0.04151756 0.07737258 0.06166794
a2 0.04680101 0.07138281 0.07005758
a3 0.98120583 0.96825317 0.96955009
a4 1.52177642 1.57192003 1.62818979

[[3]]
          [,1]      [,2]       [,3]
a1 0.1151765 0.1733337 0.07806343
a2 0.1405760 0.2070153 0.07304057
a3 0.8228107 0.9457689 1.03273787
a4 1.6026743 1.7581600 1.89761473

[[4]]
          [,1]      [,2]       [,3]
a1 0.1360939 0.1590460 0.08682901
a2 0.1520694 0.2047105 0.07762287
a3 0.9121020 1.0151550 0.96390948
a4 1.9088521 2.0688248 1.99595126

1 个答案:

答案 0 :(得分:2)

使用它来设置列表中矩阵的rownames:

jsfiddle.net/yzu1prw1/1/