0

Here's my reprex:

make_named_list <- function(x) {
  list(apple = 1, banana = 2, carrot = 3)
}

tibble(x = 1:3, y = 2:4) %>% 
  mutate(z = map(x, make_named_list)) %>% 
  unnest() %>% 
  pull(z)

The output is a list without any names, but I would like to output a named list. I had a look at a related question here, but I'm not sure the solution applies to my (simpler?) scenario.

To be clear, the desired output is:

list(apple = 1, banana = 2, carrot = 3, 
     apple = 1, carrot = 2, banana = 3, 
     apple = 1, banana = 2, carrot = 3)
M--
  • 25,431
  • 8
  • 61
  • 93
rcorty
  • 1,140
  • 1
  • 10
  • 28
  • Hi @rcorty, Could you inform the desired output? I can not identify if you wish each slot of the list to have a name (apple, banana, carrot) or something else. – Adelmo Filho Jun 20 '19 at 22:20
  • FYI the `unnest()` functions have had some updates in the development version of **tidyr** that make keeping list names easier. See some info in the [NEWS](https://github.com/tidyverse/tidyr/blob/master/NEWS.md#rectangling). – aosmith Jun 20 '19 at 22:22
  • wow, there are a lot of changes coming down the pipe (haha)...thanks for pointing this out – rcorty Jun 20 '19 at 22:25
  • I tried it with the development version of tidyr and still no names in the output – rcorty Jun 20 '19 at 22:33
  • see if this helps https://stackoverflow.com/questions/50187445/how-to-name-a-dataframe-so-that-i-can-look-for-it-within-a-list – cephalopod Jun 20 '19 at 22:35
  • Oh, I see your desired output now; I was picturing it incorrectly. I think the new functions like `unnest_longer()` would put the names in a separate column when unnesting. – aosmith Jun 20 '19 at 22:43

3 Answers3

1

Depending on how your output requirement is you can try one of the following

library(tidyverse)

tibble(x = 1:3, y = 2:4) %>% 
  mutate(z = map(x, make_named_list)) %>%
  pull(z) 

#[[1]]
#[[1]]$apple
#[1] 1

#[[1]]$banana
#[1] 2

#[[1]]$carrot
#[1] 3


#[[2]]
#[[2]]$apple
#[1] 1
#.....

Or

tibble(x = 1:3, y = 2:4) %>% 
   mutate(z = map(x, make_named_list)) %>%
   pull(z) %>% unlist() 

# apple banana carrot  apple banana carrot  apple banana carrot 
#     1      2      3      1      2      3      1      2      3 

Or

tibble(x = 1:3, y = 2:4) %>% 
   mutate(z = map(x, make_named_list)) %>%
   pull(z) %>% unlist() %>% as.list()

#$apple
#[1] 1

#$banana
#[1] 2

#$carrot
#[1] 3

#$apple
#[1] 1
#... 
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

Looks like we just need rep

rep(make_named_list(3), 3)
#$apple
#[1] 1

#$banana
#[1] 2

#$carrot
#[1] 3

#$apple
#[1] 1

#$banana
#[1] 2

#$carrot
#[1] 3

#$apple
#[1] 1

#$banana
#[1] 2

#$carrot
#[1] 3

Or if we are using tidyverse, then use flatten

tibble(x = 1:3, y = 2:4) %>% 
    transmute(z = map(x, make_named_list)) %>% 
    pull(z) %>% 
    flatten
#$apple
#[1] 1

#$banana
#[1] 2

#$carrot
#[1] 3

#$apple
#[1] 1

#$banana
#[1] 2

#$carrot
#[1] 3

#$apple
#[1] 1

#$banana
#[1] 2

#$carrot
#[1] 3
akrun
  • 874,273
  • 37
  • 540
  • 662
0

Set names_repair = tidyr_legacy in unnest().

library(tidyverse)
make_named_list <- function(x) {
  list(apple = 1, banana = 2, carrot = 3)
}

tibble(x = 1:3, y = 2:4) %>% 
  mutate(z = map(x, make_named_list)) %>% 
  unnest(cols = c(z), names_repair = tidyr_legacy) %>% 
  pull(z)
hc_haha
  • 189
  • 2
  • 4