I have a tibble that contains a list-column of data frames. In this minimal example, such tibble has 1 row only:
library(tibble)
df_meta <-
tibble(my_base_number = 5,
my_data = list(mtcars))
df_meta
#> # A tibble: 1 x 2
#> my_base_number my_data
#> <dbl> <list>
#> 1 5 <df [32 x 11]>
I want to modify the table inside my_data and mutate a new column in there. It's mtcars data, and I want to mutate a new column that takes a log of the mpg column.
Although I can do this:
library(dplyr)
library(purrr)
df_meta %>%
mutate(my_data_with_log_col = map(.x = my_data, .f = ~ .x %>%
mutate(log_mpg = map(.x = mpg, .f = ~log(.x, base = 5)))
)
)
#> # A tibble: 1 x 3
#> my_base_number my_data my_data_with_log_col
#> <dbl> <list> <list>
#> 1 5 <df [32 x 11]> <df [32 x 12]>
What I really want is that the call to log() inside inner map() will pass the value to the base argument from df_meta$my_base_number rather than the hard-coded 5 in my example.
And although in this 1-row example this simply works:
df_meta %>%
mutate(my_data_with_log_col = map(.x = my_data, .f = ~ .x %>%
mutate(log_mpg = map(.x = mpg, .f = ~log(.x, base = df_meta$my_base_number)))
)
)
consider just a bit more complicated pipe procedure where it doesn't work anymore:
tibble(my_data = rep(list(mtcars), 3)) %>%
add_column(base_number = 1:3) %>%
mutate(my_data_with_log_col = map(.x = my_data, .f = ~ .x %>%
mutate(log_mpg = map(.x = mpg, .f = ~log(.x, base = # <- ???
)))
)
)
So what I'm looking for is a procedure that allows me to "travel" up and down in the nesting hierarchy when I refer to different values that are stored in whatever construct in each row of the "meta-table".
Right now, as I go deeper with map(), to work on nested tables, I can't refer to data stored upper. If you wish, I'm looking for something analoguous to cd ../../.. when navigating with terminal.