简体   繁体   English

r-如何将多个列表列表组合到一个数据框中

[英]r - How to combine multiple lists of lists into a dataframe

I am trying to combine 2 lists of lists that are of unequal length into a data frame. 我正在尝试将长度不等的2个列表列表合并到一个数据帧中。

I am starting with a list containing 2 matrices. 我从一个包含2个矩阵的列表开始。

mtx_a <- matrix(data = c(1:24), nrow = 4)
mtx_b <- matrix(data = c(25:36), nrow = 3)
row.names(mtx_a) <- c("A", "B", "C", "D")
row.names(mtx_b) <- c("A", "B", "C")
colnames(mtx_a) <- c("V1", "V2", "W1", "W2", "X1", "X2")
colnames(mtx_b) <- c("Y1", "Y2", "Z1", "Z2")
mtx_list <- list(mtx_a, mtx_b)
names(mtx_list) <- c("mtx_a", "mtx_b")
mtx_list

I have extracted a specific row based on rowname to examine. 我已经提取了基于行名的特定行进行检查。

mtx_list <- lapply(mtx_list, function(mtx_list){
  mtx_list[row.names(mtx_list) %in% c("A"),]})

Next I extracted specific columns based on their index. 接下来,我根据它们的索引提取了特定的列。 (Alternating columns are equivalent to condition 1 and 2). (备用列等效于条件1和2)。

cond_1 <- lapply(lapply(mtx_list, function(mtx_list) {
  mtx_list[c(T,F)]}), unname)
cond_2 <- lapply(lapply(mtx_list, function(mtx_list) {
  mtx_list[c(F,T)]}), unname)

I am trying to generate a data frame that looks like this: 我正在尝试生成一个看起来像这样的数据框:

        cond_1   cond_2 
mtx_a   1        5
mtx_a   9        13
mtx_a   17       21
mtx_b   25       28
mtx_b   31       34

I have tried the following but have not have any success: 我尝试了以下方法,但没有成功:

list(cond_1, cond_2)
library(plyr)
ldply(c(cond_1, cond_2))

Extract all the "A" rows first, then just feed into a 2-column matrix: 首先提取所有"A"行,然后将其输入2列矩阵:

matrix(unlist(lapply(mtx_list, `[`, "A", )), ncol=2, byrow=TRUE)
#     [,1] [,2]
#[1,]    1    5
#[2,]    9   13
#[3,]   17   21
#[4,]   25   28
#[5,]   31   34

That can be wrapped in data.frame() if need be. 如果需要,可以将其包装在data.frame()中。

You can use Map to cbind respective list elements. 您可以使用Map cbind各个列表元素。

do.call(rbind, Map(cbind, cond_1, cond_2))
#     [,1] [,2]
#[1,]    1    5
#[2,]    9   13
#[3,]   17   21
#[4,]   25   28
#[5,]   31   34

A little more work can give you slightly better result 多做一些工作可以为您带来更好的结果

data.frame(mtx = unlist(lapply(1:length(cond_1),
                               function(i)
                                   rep(names(cond_1)[i], length(cond_1[[i]])))),
    do.call(rbind, Map(cbind, cond_1, cond_2)))
#    mtx X1 X2
#1 mtx_a  1  5
#2 mtx_a  9 13
#3 mtx_a 17 21
#4 mtx_b 25 28
#5 mtx_b 31 34

Here is an alternative using sapply . 这是使用sapply的替代方法。 I wrapped it in a function for easier repeated use. 我将其包装在一个函数中,以方便重复使用。

getter <- function(rowName) {
  # loop through matrices in list, pull out rows that match name
  temp <- unlist(sapply(mtx_list, function(x) x[rowName,]), use.names=FALSE)
  # return data.frame with alternating values
  data.frame(var1=temp[c(TRUE, FALSE)], var2=temp[c(FALSE, TRUE)])
}

getter("A")
  var1 var2
1    1    5
2    9   13
3   17   21
4   25   28
5   31   34

Note that it is necessary that all matrices in the list contain the row name for this function to work. 请注意,列表中的所有矩阵都必须包含行名称,此功能才能起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM