How to convert list list to slice (dataframe)

I have the following list. It contains two variables: a pair and genes. The content is pairalways a two-line vector. A variable genesis a vector that can contain more than 1 value.


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"

How can I convert it to this data framework:

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

Note that a variable genesis a vector of more than one row.

My best attempt is that which does not give what I want:

> 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

Update

In a new example to display vector content 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"

Expected Result:

pair1         pair2        genes_vec
BoneMarrow    Pulmonary    PRR11,GNB2L1
BoneMarrow    Umbilical    GNB2L1
Pulmonary     Umbilical    ATP1B1
+4
source share
4 answers

Using tidyverse, you can use purrrto help you


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_chr(lol, "genes") map(lol2, "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]>

,

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
+3
> 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
+1

EDIT: Updated to work with vector lol2.

Maybe so:

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
+1
source

This should work:

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

In the same way that you treat genes as a vector, you can also handle pairs as a vector. Instead of pairs 1 and 2, you simply use both of them.

0
source

Source: https://habr.com/ru/post/1682814/


All Articles