如何将列表的列表转换为Tibble( Dataframe )

z8dt9xmd  于 2023-04-18  发布在  其他
关注(0)|答案(6)|浏览(149)

我有下面的列表列表。它包含两个变量:pair和gene的关系,pair的包含总是两个字符串的向量,而变量genes是一个可以包含多于1个值的向量。

lol <- list(structure(list(pair = c("BoneMarrow", "Pulmonary"), genes = "PRR11"), .Names = c("pair", 
"genes")), structure(list(pair = c("BoneMarrow", "Umbilical"), 
    genes = "GNB2L1"), .Names = c("pair", "genes")), structure(list(
    pair = c("Pulmonary", "Umbilical"), genes = "ATP1B1"), .Names = c("pair", 
"genes")))

lol
#> [[1]]
#> [[1]]$pair
#> [1] "BoneMarrow" "Pulmonary" 
#> 
#> [[1]]$genes
#> [1] "PRR11"
#> 
#> 
#> [[2]]
#> [[2]]$pair
#> [1] "BoneMarrow" "Umbilical" 
#> 
#> [[2]]$genes
#> [1] "GNB2L1"
#> 
#> 
#> [[3]]
#> [[3]]$pair
#> [1] "Pulmonary" "Umbilical"
#> 
#> [[3]]$genes
#> [1] "ATP1B1"

如何将其转换为dataframe:

pair1         pair2        genes_vec
BoneMarrow    Pulmonary    PRR11
BoneMarrow    Umbilical    GNB2L1
Pulmonary     Umbilical    ATP1B1

请注意,genes变量是一个向量,而不是一个字符串。
我最好的尝试是这样的,这并没有给予我想要的:

> do.call(rbind, lapply(lol, data.frame, stringsAsFactors=FALSE))
        pair  genes
1 BoneMarrow  PRR11
2  Pulmonary  PRR11
3 BoneMarrow GNB2L1
4  Umbilical GNB2L1
5  Pulmonary ATP1B1
6  Umbilical ATP1B1

更新

使用新示例显示genes的矢量内容

lol2 <- list(structure(list(pair = c("BoneMarrow", "Pulmonary"), genes = c("GNB2L1", 
"PRR11")), .Names = c("pair", "genes")), structure(list(pair = c("BoneMarrow", 
"Umbilical"), genes = "GNB2L1"), .Names = c("pair", "genes")), 
    structure(list(pair = c("Pulmonary", "Umbilical"), genes = "ATP1B1"), .Names = c("pair", 
    "genes")))

lol2
#> [[1]]
#> [[1]]$pair
#> [1] "BoneMarrow" "Pulmonary" 
#> 
#> [[1]]$genes
#> [1] "GNB2L1" "PRR11" 
#> 
#> 
#> [[2]]
#> [[2]]$pair
#> [1] "BoneMarrow" "Umbilical" 
#> 
#> [[2]]$genes
#> [1] "GNB2L1"
#> 
#> 
#> [[3]]
#> [[3]]$pair
#> [1] "Pulmonary" "Umbilical"
#> 
#> [[3]]$genes
#> [1] "ATP1B1"

预期输出为:

pair1         pair2        genes_vec
BoneMarrow    Pulmonary    PRR11,GNB2L1
BoneMarrow    Umbilical    GNB2L1
Pulmonary     Umbilical    ATP1B1
0lvr5msh

0lvr5msh1#

使用tidyverse,您可以使用purrr来帮助您

library(dplyr)
library(purrr)

tibble(
  pair = map(lol, "pair"),
  genes_vec = map_chr(lol, "genes")
) %>% 
  mutate(
    pair1 = map_chr(pair, 1),
    pair2 = map_chr(pair, 2) 
  ) %>%
  select(pair1, pair2, genes_vec)
#> # A tibble: 3 x 3
#>        pair1     pair2 genes_vec
#>        <chr>     <chr>     <chr>
#> 1 BoneMarrow Pulmonary     PRR11
#> 2 BoneMarrow Umbilical    GNB2L1
#> 3  Pulmonary Umbilical    ATP1B1

在第二个例子中,只要用map(lol2, "genes")替换map_chr(lol, "genes"),因为你想保持一个嵌套的数据框和一个列表列。

tibble(
  pair = map(lol2, "pair"),
  genes_vec = map(lol2, "genes")
) %>% 
  mutate(
    pair1 = map_chr(pair, 1),
    pair2 = map_chr(pair, 2) 
  ) %>%
  select(pair1, pair2, genes_vec)
#> # A tibble: 3 x 3
#>        pair1     pair2 genes_vec
#>        <chr>     <chr>    <list>
#> 1 BoneMarrow Pulmonary <chr [2]>
#> 2 BoneMarrow Umbilical <chr [1]>
#> 3  Pulmonary Umbilical <chr [1]>

一种更通用的方法是使用嵌套的tibles,并根据需要取消嵌套

library(dplyr)
library(purrr)
library(tidyr)

tab1 <-lol %>%
  transpose() %>%
  as_tibble() %>%
  mutate(pair = map(pair, ~as_tibble(t(.x)))) %>%
  mutate(pair = map(pair, ~set_names(.x, c("pair1", "pair2"))))
tab1
#> # A tibble: 3 x 2
#>               pair     genes
#>             <list>    <list>
#> 1 <tibble [1 x 2]> <chr [1]>
#> 2 <tibble [1 x 2]> <chr [1]>
#> 3 <tibble [1 x 2]> <chr [1]>

对于lol2,除非列表lol2而不是lol1,否则没有任何变化

tab2 <- lol2 %>%
  transpose() %>%
  as_tibble() %>%
  mutate(pair = map(pair, ~as_tibble(t(.x)))) %>%
  mutate(pair = map(pair, ~set_names(.x, c("pair1", "pair2"))))
tab2
#> # A tibble: 3 x 2
#>               pair     genes
#>             <list>    <list>
#> 1 <tibble [1 x 2]> <chr [2]>
#> 2 <tibble [1 x 2]> <chr [1]>
#> 3 <tibble [1 x 2]> <chr [1]>

然后,您可以解嵌套所需的列

tab1 %>%
  unnest()
#> # A tibble: 3 x 3
#>    genes      pair1     pair2
#>    <chr>      <chr>     <chr>
#> 1  PRR11 BoneMarrow Pulmonary
#> 2 GNB2L1 BoneMarrow Umbilical
#> 3 ATP1B1  Pulmonary Umbilical

tab2 %>% 
  unnest(pair)
#> # A tibble: 3 x 3
#>       genes      pair1     pair2
#>      <list>      <chr>     <chr>
#> 1 <chr [2]> BoneMarrow Pulmonary
#> 2 <chr [1]> BoneMarrow Umbilical
#> 3 <chr [1]>  Pulmonary Umbilical
yyhrrdl8

yyhrrdl82#

对于第一个问题,与其他答案几乎相同,略短/更紧凑:

library(tidyverse)
lol <- list(structure(list(pair = c("BoneMarrow", "Pulmonary"), genes = "PRR11"),
                      .Names = c("pair", "genes")),
            structure(list(pair = c("BoneMarrow", "Umbilical"), genes = "GNB2L1"),
                      .Names = c("pair", "genes")),
            structure(list(pair = c("Pulmonary", "Umbilical"), genes = "ATP1B1"), .Names = c("pair","genes")))

map_dfr(lol, ~as_tibble(.) %>% 
          mutate(row=paste0("pair", row_number()))%>% 
          spread(row, pair) %>% 
          select(pair1, pair2, genes))
#> # A tibble: 3 x 3
#>   pair1      pair2     genes 
#>   <chr>      <chr>     <chr> 
#> 1 BoneMarrow Pulmonary PRR11 
#> 2 BoneMarrow Umbilical GNB2L1
#> 3 Pulmonary  Umbilical ATP1B1

创建于2020-12-04由reprex package(v0.3.0)

polhcujo

polhcujo3#

编辑:更新以使用矢量lol2。
也许像这样:

as.data.frame(do.call(rbind,lapply(lol2, function(x) {c(unlist(x[1]),gene=paste(unlist(x[2]),collapse=","))})),stringsAsFactors = F)



       pair1     pair2         genes
1 BoneMarrow Pulmonary GNB2L1, PRR11
2 BoneMarrow Umbilical        GNB2L1
3  Pulmonary Umbilical        ATP1B1
qpgpyjmq

qpgpyjmq4#

这应该可以工作:

data.frame(do.call(rbind,lol2))
data.frame(do.call(rbind,lol2))
                   pair         genes
1 BoneMarrow, Pulmonary GNB2L1, PRR11
2 BoneMarrow, Umbilical        GNB2L1
3  Pulmonary, Umbilical        ATP1B1

将基因视为载体的方式与将基因对视为载体的方式相同:而不是对1和2,您只需使用它们两者。

xu3bshqb

xu3bshqb5#

> lol1 <- data.frame(t(sapply(lol,c)))
> as.data.frame(t(apply(lol1, 1, unlist)))
       pair1     pair2  genes
1 BoneMarrow Pulmonary  PRR11
2 BoneMarrow Umbilical GNB2L1
3  Pulmonary Umbilical ATP1B1
xwmevbvl

xwmevbvl6#

另一个tidyverse解决方案:版本lol得到与OP中相同的结果。lol2将基因载体分成适当数量的列:

lol2 <- list(structure(list(pair = c("BoneMarrow", "Pulmonary"), genes = c("GNB2L1", 
"PRR11")), .Names = c("pair", "genes")), structure(list(pair = c("BoneMarrow", 
"Umbilical"), genes = "GNB2L1"), .Names = c("pair", "genes")), 
    structure(list(pair = c("Pulmonary", "Umbilical"), genes = "ATP1B1"), .Names = c("pair", 
    "genes")))

lol2_result <- lol2 |> 
    purrr::transpose() |> 
    tibble::as_tibble() |> 
    tidyr::unnest_wider(col = c(pair, genes), names_sep = "_")
lol2_result
#> # A tibble: 3 × 4
#>   pair_1     pair_2    genes_1 genes_2
#>   <chr>      <chr>     <chr>   <chr>  
#> 1 BoneMarrow Pulmonary GNB2L1  PRR11  
#> 2 BoneMarrow Umbilical GNB2L1  <NA>   
#> 3 Pulmonary  Umbilical ATP1B1  <NA>

创建于2023-04-13带有reprex v2.0.2

相关问题