-1

I'm struggling with this problem:

I have three lists of dataframes (each list has the same number of rows, each dataframe has 3 columns). I want to combine the lists into one nested dataframe, such that there are 3 columns (one column for a list).

Sample code:

a_list_of_dfs <- lapply(seq_along(a_list), function(i) {
  
  tibble(x = a_list[[i]]$x,
         y = a_list[[i]]$y,
         z = a_list[[i]]$z)
})

b_list_of_dfs <- lapply(seq_along(b_list), function(i) {
  
  tibble(x = b_list[[i]]$x,
         y = b_list[[i]]$y,
         z = b_list[[i]]$z)
})

c_list_of_dfs <- lapply(seq_along(c_list), function(i) {
  
  tibble(x = c_list[[i]]$x,
         y = c_list[[i]]$y,
         z = c_list[[i]]$z)
})

nested_df <- tibble(a=a_list_of_df,
                    b=b_list_of_df,
                    c=c_list_of_df)

The problem is, this solution doesn't give me the result I want. The idea is to be able to access the nested dataframe like this:

nested_df$a$x

How can I change that to get the result I want?

Edit:
a_list, b_list and c_list are nested lists where each element has some string attributes. For example, a_list has X rows and each row has attributes name, label and value. So I want to achieve a solution where I can just access the nested dataframe like this:

nested_df$a$name  # and value etc.

The result of the dput function below (kept only the first two elements, the strcture is the same across a list):

`list(structure(list(name = "VS", label = "VS", 
    value = "SUM"), .Names = c("name", 
"label", "value")), structure(list(name = "VP", 
    label = "VP", value = "SUM"), .Names = c("name", 
"label", "value")),`

My solution
Passing a dataframe instead of a list of 1-row dataframes as a column worked for me.

D3nz13
  • 74
  • 5
  • 2
    Can you add an example of the contents for `a_list`, `b_list` and `c_list`? – VFreguglia May 14 '22 at 17:37
  • @Freguglia Just added more information. – D3nz13 May 14 '22 at 18:23
  • 3
    You will need to actually provide the result of `dput(a_list)` in your solution. The way I see it, `nested_df` is a three column df, with columns a,b,c, where each column is a list of tibbles. The elements of list `nested_df$a` are unnamed, so `nested_df$a$name` probably is returning NULL for you. – langtang May 14 '22 at 20:13
  • @langtang I added the result. When I try to, for example, access it like `nested_df$a$name` it doesn't work but when I do `nested_df$a[[1]]$name` then it does – D3nz13 May 15 '22 at 12:31
  • exactly. because the elements of the list of tibbles in `nested_df$a` are not named. They can be accessed using `[[1]]`, `[[2]]`, etc. Each of those (unnamed) elements is itself a dataframe, with named columns such as `name`, `value`, `label`, etc.. which you can acess as you do above (i.e. `nested_df$a[[1]]$name`) – langtang May 15 '22 at 13:35

1 Answers1

0

The structure of your data is not very clear because your sample is a bit small. But does this do what you want?

library(tidyverse)  
df <- list(structure(list(name = "VS", label = "VS", value = "SUM"), 
                     .Names = c("name", "label", "value")), 
           structure(list(name = "VP", label = "VP", value = "SUM"), 
                  .Names = c("name", "label", "value")))

df <- df %>% set_names(c("a", "b"))

df$a$name
df %>% as_tibble()

You should name the list elements.

Michael Dewar
  • 2,553
  • 1
  • 6
  • 22
  • Not quite. The dput result shows two list elements (there are hundreds of elements). What I want to achieve is to, since all elements in that lists are tibbles, I want each of the elements to become a single row and each list to become a single column. So let's say each list has 100 elements, the output dataframe should have 100 rows and 3 columns since there are 3 lists. – D3nz13 May 15 '22 at 16:11