如何提取列表列表中所有列表的行名并将其存储在新数据框或列表中

时间:2019-04-01 10:59:57

标签: r list dataframe for-loop

我有一个清单清单。下面给出了一个类似的玩具示例。我想从每个列表中提取行名,然后将这些行名存储在新的数据框或新的列表列表中(与原始结构相同)。

理想情况下,名称或新列表名称将与列表列表中的列表名称相同。

注意。列表长度各不相同,必须考虑在内。我宁愿不使用N / A填充空白。


dput(head(Chars_alive)):

list(FEB_games = list(GAME1 = structure(list(GAME1_Class = structure(c(2L, 
1L, 5L, 4L, 3L), .Label = c("fighter", "paladin", "rouge", "sorcerer", 
"wizard"), class = "factor"), GAME1_Race = structure(c(3L, 1L, 
4L, 3L, 2L), .Label = c("elf", "gnome", "human", "orc"), class = "factor"), 
GAME1_Alignment = structure(c(4L, 2L, 1L, 5L, 3L), .Label = c("CE", 
"CG", "LG", "NE", "NN"), class = "factor"), GAME1_Level = c(6, 
7, 6, 7, 7), GAME1_Alive = structure(c(1L, 1L, 1L, 1L, 1L
), .Label = "y", class = "factor")), row.names = c("Stan", 
"Kenny", "Cartman", "Kyle", "Butters"), class = "data.frame"), 
GAME2 = structure(list(GAME2_Class = structure(c(5L, 2L, 
4L, 1L), .Label = c("bard", "cleric", "fighter", "monk", 
"wizard"), class = "factor"), GAME2_Race = structure(c(3L, 
2L, 4L, 1L), .Label = c("dwarf", "elf", "half-elf", "human"
), class = "factor"), GAME2_Alignment = structure(c(2L, 1L, 
5L, 3L), .Label = c("CE", "CG", "LG", "NE", "NN"), class = "factor"), 
    GAME2_Level = c(5, 5, 5, 5), GAME2_Alive = structure(c(2L, 
    2L, 2L, 2L), .Label = c("n", "y"), class = "factor")), row.names = c("Kenny", 
"Cartman", "Kyle", "Butters"), class = "data.frame")), MAR_games = list(
GAME3 = structure(list(GAME3_Class = structure(c(2L, 1L, 
5L, 3L), .Label = c("barbarian", "cleric", "monk", "ranger", 
"warlock"), class = "factor"), GAME3_Race = structure(c(2L, 
3L, 2L, 1L), .Label = c("dwarf", "elf", "half-elf", "human"
), class = "factor"), GAME3_Alignment = structure(c(2L, 2L, 
1L, 2L), .Label = c("CE", "LG", "LN"), class = "factor"), 
    GAME3_Level = c(1, 1, 1, 1), GAME3_Alive = structure(c(2L, 
    2L, 2L, 2L), .Label = c("n", "y"), class = "factor")), row.names = c("Stan", 
"Kenny", "Cartman", "Butters"), class = "data.frame"), GAME4 = structure(list(
    GAME4_Class = structure(c(1L, 5L, 4L, 3L), .Label = c("fighter", 
    "paladin", "rouge", "sorcerer", "wizard"), class = "factor"), 
    GAME4_Race = structure(c(3L, 2L, 4L, 1L), .Label = c("dwarf", 
    "elf", "half-elf", "human"), class = "factor"), GAME4_Alignment = structure(c(2L, 
    1L, 4L, 3L), .Label = c("CE", "CG", "LG", "LN"), class = "factor"), 
    GAME4_Level = c(5, 5, 5, 5), GAME4_Alive = structure(c(2L, 
    2L, 2L, 2L), .Label = c("n", "y"), class = "factor")), row.names = c("Kenny", 
"Cartman", "Kyle", "Butters"), class = "data.frame")))

as.data.frame(rownames(Chars_alive[[1]][[1]])) -> GAME1
as.data.frame(rownames(Chars_alive[[2]][[1]])) -> GAME2

因为GAME1和GAME2的长度不同,所以数据帧可能不是理想的(我的实际数据在列表列表之间的长度差异很大)。

for (i in Chars_alive) {
  for (j in i)
    rownames(j) -> x
}

for循环可以工作,但是我是循环的新手,不知道如何将所有第j个元素放入一个新的数据帧或列表中。

ls2 <- list(Game1 <- rownames(Chars_alive[[1]][[1]]), Game2 <- rownames(Chars_alive[[1]][[2]]),
                 Game3 <- rownames(Chars_alive[[2]][[1]]), Game4 <- rownames(Chars_alive[[2]][[2]]))

也许直接制作一个新列表是可行的,但是如果是这种情况,我想保留原始列表的结构,即FEB_games> GAME1,GAME2和MAR_games> GAME3,GAME4。另外,我希望列表名称保持相同,即GAME1,GAME2,GAME3和GAME4。


理想的输出将是一个数据帧:

    GAME1    GAME2    GAME3    GAME4
1   Stan     Kenny    Stan     Kenny
2   Kenny    Cartman  Kenny    Cartman
3   Cartman  Kyle     Cartman  Kyle 
4   Kyle     Butters  Butters  Butters
5   Butters   

或列表:

Listname
    FEB_games
        GAME1
           'Stan', 'Kenny', 'Cartman', 'Kyle', 'Butters'
        GAME2
           'Kenny', 'Cartman', 'Kyle', 'Butters'
    MAR_games
        GAME3
            'Stan', 'Kenny', 'Cartman', 'Butters'
        GAME4
            'Kenny', 'Cartman', 'Kyle', 'Butters'

1 个答案:

答案 0 :(得分:2)

你好,我会像这样在lapply中使用lapply。我将您的列表称为“ list_games”。

lapply(list_games, function(x){lapply(x, row.names)})

这给你

$FEB_games
$FEB_games$GAME1
[1] "Stan"    "Kenny"   "Cartman" "Kyle"    "Butters"

$FEB_games$GAME2
[1] "Kenny"   "Cartman" "Kyle"    "Butters"


$MAR_games
$MAR_games$GAME3
[1] "Stan"    "Kenny"   "Cartman" "Butters"

$MAR_games$GAME4
[1] "Kenny"   "Cartman" "Kyle"    "Butters"

如果行名的长度相同,则可以使用

将其保存为data.frame
do.call("rbind.data.frame", lapply(list_games, function(x){lapply(x, row.names)}))

这在这里不起作用,因为行名的长度不一样。在这种情况下,您可以执行以下操作:

res <- sapply(list_games, function(x){lapply(x, row.names)})
n.obs <- sapply(res , length)
seq.max <- seq_len(max(n.obs))
df <- data.frame(t(sapply(res, "[", i = seq.max)))
df
     X1      X2      X3      X4      X5
1  Stan   Kenny Cartman    Kyle Butters
2 Kenny Cartman    Kyle Butters    <NA>
3  Stan   Kenny Cartman Butters    <NA>
4 Kenny Cartman    Kyle Butters    <NA>

如果您需要进一步的解释,请告诉我。最后一部分就像here