Using tidyverse, you could use purrr to 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
with the second example, just replace map_chr(lol, "genes") with map(lol2, "genes") as you want to keep a nested dataframe with a list column.
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]>
And a more generic approach would be to work with nested tibbles and unnest them as needed
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]>
For lol2 nothing changes unless the list lol2 instead of 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]>
You can then unnest what the column you want
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
Answer from cderv on Stack OverflowUsing tidyverse, you could use purrr to 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
with the second example, just replace map_chr(lol, "genes") with map(lol2, "genes") as you want to keep a nested dataframe with a list column.
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]>
And a more generic approach would be to work with nested tibbles and unnest them as needed
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]>
For lol2 nothing changes unless the list lol2 instead of 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]>
You can then unnest what the column you want
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
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
The way you treat the genes as a vector is the same way you can treat the pairs as a vector: instead of pair 1 and 2 you just use both of them.
[Q] an efficient way to convert lists nested within a list to data.frame?
r - How to convert a list of tibbles/dataframes into a nested tibble/dataframe - Stack Overflow
Combine a list of tibbles into a document term matrix
convert such tibble to data frame
Hi all,
Say I have a list object, where each element is a 'person' who is identifiable by their ID number. Then within each person is another list object which contains the actual vector of data.
Example: say I have ID numbers for person "AA001", "BB001", "CC001"
my_list = list( AA001 = list(x = 1:5, y = 1:5), BB001 = list(x = -1:-5, y = -1:-5), CC001 = list(x = rep(99, 5), y = rep(0, 5)) ) print(my_list)
My desired output is something like
my_df = data.frame(
ID = rep(c("AA001", "BB001", "CC001"), each = 5),
x = c(1:5, -1:-5, rep(99, 5)),
y = c(1:5, -1:-5, rep(0, 5))
)
print(my_df)If possible, I would prefer efficiency over readability as I will be running some simulations with thousands for IDs and thousands of runs.
As a side question, the way my_list is structured. Is that even a good way to store data?
Thank you in advance!
We may use enframe
library(tibble)
enframe(ex_list)
# A tibble: 2 x 2
name value
<chr> <list>
1 a <tibble [4 × 2]>
2 b <df [32 × 11]>
If we need to change the column names, use the name and value
> enframe(ex_list, name = 'data_name', value = 'data')
# A tibble: 2 x 2
data_name data
<chr> <list>
1 a <tibble [4 × 2]>
2 b <df [32 × 11]>
Is this what you want?
library(tidyverse)
lapply(ex_list, nest) %>%
dplyr::bind_rows(., .id = "data_name")
# # A tibble: 2 x 2
# data_name data
# <chr> <list>
# 1 a <tibble [4 x 2]>
# 2 b <tibble [32 x 11]>
#OR map
#map(ex_list, nest) %>%
# bind_rows(., .id = "data_name")
Good evening everyone,
I'm struggling with transforming a list composed of tibbles (tokens and their frequencies) into a document term matrix, I mean, a matrix with documents per row and terms per column.
Think in something like this, for instance:
library(tibble) # some mock up data doc1 <- tribble( ~token, ~frequency, "A", 65, "B", 62, "C", 51, ) doc2 <- tribble( ~token, ~frequency, "A", 75, "B", 64, "C", 56, ) doc3 <- tribble( ~token, ~frequency, "A", 81, "B", 62, "C", 57, ) # Combine the documents into a list my_list <- list(doc1, doc2, doc3)
I tried something like this:
# Convert each tibble into a document term matrix
dtm_list <- lapply(my_list, function(x) {
cast_sparse(x, token, frequency)
})
# Combine the document term matrices into a single sparse matrix
dtm <- Reduce(`+`, dtm_list)But matrices must have the same dimensions and I stuck there.
Thank you in advance :)