Search code examples
rdplyrtibble

Assign tibble as a nested cell within tibble


Given two tibbles, I want to assign one tibble to be a cell in the other tibble. However, the current approach consistently converts my sub-tibble to a different format, which leads to errors.

I know what I want to do is possible:

library(dplyr)

desired = tibble(
  row = 1:4,
  nested = list(
    tibble(text = c("a","b","c")),
    tibble(numbers = 1:4),
    tibble(label = c("high", "med", "low"), value = c(10, 5, 0)),
    NA
  )
)

This code produces a tibble with two columns. The second column contains tibbles of different sizes and contents.

However, I can not construct this in incremental steps:

library(dplyr)

# outer tibble
df = tibble(row = 1:4)
# inner tibbles
t1 = tibble(text = c("a","b","c"))
t2 = tibble(numbers = 1:4)
t3 = tibble(label = c("high", "med", "low"), value = c(10, 5, 0))

# setup column of type tibble to assign into
df = df %>%
  mutate(nested = purrr::map(row, ~{ tibble() }))

# assign (fails)
df$nested[1] = t1 # no error but df$nested[1] is of type character not tibble
df$nested[2] = t2 # no error but df$nested[2] is of type int not tibble
df$nested[3] = t3 # gives warning and only one column of t3 is assigned

The warning I receive is:

In df$nested[3] = t3 : number of items to replace is not a multiple of replacement length

The result of this approach looks like:

> df
# A tibble: 4 x 2
    row nested          
  <int> <list>          
1     1 <chr [3]>       
2     2 <int [4]>       
3     3 <chr [3]>       
4     4 <tibble [0 × 0]>

How can I assign (inner) tibbles to individual cells in an existing (outer) tibble, so that I end up with neested tibbles?


Solution

  • Using [[ instead of [ you could do:

    library(dplyr, w = FALSE)
    
    df <- tibble(row = 1:4, nested = vector("list", 4))
    
    df$nested[[1]] <- t1
    df$nested[[2]] <- t2
    df$nested[[3]] <- t3
    df$nested[[4]] <- NA
    
    df
    #> # A tibble: 4 × 2
    #>     row nested          
    #>   <int> <list>          
    #> 1     1 <tibble [3 × 1]>
    #> 2     2 <tibble [4 × 1]>
    #> 3     3 <tibble [3 × 2]>
    #> 4     4 <lgl [1]>