拼合R中的列表时维护名称

时间:2018-08-28 11:15:05

标签: r list matrix names

下面是一些伪数据和代码。我想我正尝试做与this question.

相反的事情

操纵a statistical test之后,剩下的清单看起来很丑。当我解压缩此列表as per this method with unlist时,存储在名称中的变量/国家/地区代码完全丢失。

我想要一个数据框,其中国家作为行名,类别作为列名。

任何指针将不胜感激!

set.seed(1)

CVtest <- list()

for (i in c("AT","BE","DE")) { #For all countries

  # Test all categories together

  CVtest[[i]][["EveryCat"]] <-  sample(seq(0, 1, by=0.01), 1)

    for (j in c("All", "Sub", "Key", "Sel")) { # Test each category

      CVtest[[i]][[j]] <- sample(seq(0, 1, by=0.01), 1)
    }
}

当前输出:

> class(CVtest)
[1] "list"
> CVtest

$`AT` EveryCat      All      Sub      Key      Sel 
    0.26     0.37     0.57     0.91     0.20 

$BE EveryCat      All      Sub      Key      Sel 
    0.90     0.95     0.66     0.63     0.06 

$DE EveryCat      All      Sub      Key      Sel 
    0.20     0.17     0.69     0.38     0.77

所需的输出

> class(CVtest)
[1] "data.frame"
> CVtest
       EveryCat  All  Sub  Key  Sel
AT     0.26 0.37 0.57 0.91 0.20
BE     0.90 0.95 0.66 0.63 0.06
DE     0.20 0.17 0.69 0.38 0.77

1 个答案:

答案 0 :(得分:0)

您的问题仍然不清楚(请参阅我的最新评论)。我只能猜你想要什么。您可以尝试:

x <- purrr::transpose(CVtest)
y <- rlist::list.stack(x)
rownames(y) <- names(x)
y

请问y是否是所需的输出。否则,我将删除此答案,并请澄清您的问题。

相关问题