我有一个2D矩阵,我想将其转换为3D数组,但我没有正确地进行转换.

我的目标是让它工作,这样,无论我有多少第三维度的水平,它都会递归地工作.

以下是数据:

dfs <- (structure(c(-0.046, 0.676, -0.698, -0.047, 0.719, -0.646, -0.044, 
                             0.706, -0.667, 0.046, 0.769, -0.605, 0.047, 0.813, -0.551, 0.044, 
                             0.795, -0.578), dim = c(9L, 2L), dimnames = list(c("snatch:(Intercept)", 
                                                                                "snatch:weight", "snatch:age", "clean:(Intercept)", "clean:weight", 
                                                                                "clean:age", "total:(Intercept)", "total:weight", "total:age"
                             ), c("2.5 %", "97.5 %"))))

我要找的是这样的东西

$a
             X2.5%  X97.5%
(Intercept) -0.046   0.046
weight       0.676   0.769
age         -0.698  -0.605

$b
             X2.5%  X97.5%
(Intercept)  -0.047   0.047
weight        0.719   0.813
age          -0.646  -0.551

$c
             X2.5%  X97.5%
(Intercept)  -0.044   0.044
weight        0.706   0.795
age          -0.667  -0.578

我的 playbook 和我到目前为止所做的

rnames <- c("(Intercept)", "snatch", "age")
cnames <- colnames(dfs); cnames
lnames <- c("a", "b", "c"); lnames

array(
  data = matrix(data = type.convert(gsub("^[^:]+:\\s*", "", dfs), as.is=TRUE),
                nrow=nrow(dfs), ncol=ncol(dfs)),
  dim = c(nrow(dfs)/length(lnames), length(cnames), length(lnames)),
  dimnames = list(rows=rnames,
                  column=cnames,
                  level=lnames)) %>%
  apply(., 2, data.frame)

推荐答案

你可以试试split.data.frame,例如,

> split.data.frame(dfs, gsub(":.*", "", row.names(dfs)))
$clean
                   2.5 % 97.5 %
clean:(Intercept) -0.047  0.047
clean:weight       0.719  0.813
clean:age         -0.646 -0.551

$snatch
                    2.5 % 97.5 %
snatch:(Intercept) -0.046  0.046
snatch:weight       0.676  0.769
snatch:age         -0.698 -0.605

$total
                   2.5 % 97.5 %
total:(Intercept) -0.044  0.044
total:weight       0.706  0.795
total:age         -0.667 -0.578

R相关问答推荐

在与ggplot 2和网格的最佳匹配线上绘制箭头

如何从使用lapply()的r中的拆分数据帧中删除多个部分?

如何在弹性表中为类别值的背景上色

如何在四进制仪表板值框中显示值(使用shiny 的服务器计算)

对lme 4对象运行summary()时出错(diag中的错误(from,names = RST):对象unpackedMatrix_diag_get找不到)

为什么st_join(ob1,ob2,left = True)返回具有比ob1更多功能的sf对象?

R:连接值,而不是变量?

r—绘制相交曲线

如何在xyplot中 for each 面板打印R^2

更改默认系列1以更改名称

R s iml包如何处理语法上无效的因子级别?'

是否可以创建一个ggplot与整洁判断的交互作用

我如何才能找到FAMILY=POISSON(LINK=&Q;LOG&Q;)中的模型预测指定值的日期?

计算满足R中条件的连续列

在R函数中使用加号

更改STAT_VALLES/STAT_PEAKS中的箭头线宽/大小

R如何计算现有行的总和以添加新的数据行

扩展R中包含列表的数据框

我如何使用tidyselect来传递一个符号数组,比如Pivot_Long?

我已经运行了几个月的代码的`Palette()`中出现了新的gglot错误