0
votes

Stating from the following tbl_list

tbl1 = tibble(a = c(1, 2), b = c(3, 4))
tbl2 = tibble(a = c(10, 20), b = c(30, 40))
tbl_list = list(tbl1, tbl2)
tbl_names = c("tbl1", "tbl2")
names(tbl_list) = tbl_names

My goal is

   e_tbl_list = list(tibble(a = c(1, 2), 
                         b = c(3, 4), 
                         tbl_name = c("tbl1", "tbl1")), 
                  tibble(a = c(10, 20), 
                         b = c(30, 40), 
                         tbl_name = c("tbl2", "tbl2"))
                  ) 

However , it doesn't work when using this code

 a_tbl_list = tbl_names %>% map(~ tbl_list[[.]] %>% mutate(tbl_name = .))
1

1 Answers

0
votes

You can use purrr's imap :

library(purrr)
imap(tbl_list, ~.x %>% dplyr::mutate(tbl_name = .y))

#$tbl1
# A tibble: 2 x 3
#      a     b tbl_name
#  <dbl> <dbl> <chr>   
#1     1     3 tbl1    
#2     2     4 tbl1    

#$tbl2
# A tibble: 2 x 3
#      a     b tbl_name
#  <dbl> <dbl> <chr>   
#1    10    30 tbl2    
#2    20    40 tbl2    

Or Map in base R :

Map(cbind, tbl_list, tbl_name = names(tbl_list))
#purrr equivalent
#map2(tbl_list, names(tbl_list), cbind)

Using lapply

lapply(seq_along(tbl_list), function(x) 
       cbind(tbl_list[[x]], tbl_name = names(tbl_list)[x]))
#purrr equivalent
#map(seq_along(tbl_list), ~cbind(tbl_list[[.x]], tbl_name = names(tbl_list)[.x]))