Mapping functions within one df to another

purrr

#1

Suppose I have a toy df with a column of unique functions (fun) like so:

df_1 <- tibble(values = rep(2,3), exponent = 1:3) %>% 
    mutate(fun = map(.x = exponent, .f = function(exponent) { function(x){}})) # creating an closure "fun"

and further, that I have a list of dfs, with the list having the same # of elements as df_1:

list_o_dfs <- list(tibble(var = 1:10), tibble(var = 3:5), tibble(var = rep(2,5)))

What I want to do: map the fun column of functions in df_1 to var in each tibble of list_o_dfs to create a new column

here’s a sketch of how I’m thinking about the syntax, which doesn’t work:

list_o_dfs %>% enframe() %>% mutate(new_var = map(.x = var, .f = df_1$fun))

It feels like pmap may be the solution here, possibly after enframe-ing/nesting list_o_dfs, and then binding columns to make one nested df, but that isn’t immediately obvious to me and attempts have likewise failed.

Any advice is much appreciated.


#2

Please provide a reproducible example (reprex). This should include a self contained example of your input, code, output, and any errors you are currently using.

You can check out this blog post to see how to create one:

As a side note, you will have to install the github version for now using devtools::install_github("tidyverse/reprex")


#3

As @tbradley suggested you need to provide us a reprex that makes clear what you are trying to do.

For example the map function you are using to create df_1 doesn’t seem to produce anything useful. exponent in .f = function(exponent) does not refer to anything in the tibble being passed into map, it is just a placeholder argument name and it doesn’t matter what name you use there. For example here is the equivalent of what the map function is producing.

suppressPackageStartupMessages(library(tidyverse))
map(.x = 1:3, .f = function(exponent) { function(x){}})
#> [[1]]
#> function (x) 
#> {
#> }
#> <environment: 0x7fc2acf6b0d0>
#> 
#> [[2]]
#> function (x) 
#> {
#> }
#> <bytecode: 0x7fc2ad0715f0>
#> <environment: 0x7fc2acf6aed8>
#> 
#> [[3]]
#> function (x) 
#> {
#> }
#> <bytecode: 0x7fc2ad0715f0>
#> <environment: 0x7fc2ad076318>
map(.x = 1:3, .f = function(e) { function(x){}})
#> [[1]]
#> function (x) 
#> {
#> }
#> <environment: 0x7fc2ad0daca0>
#> 
#> [[2]]
#> function (x) 
#> {
#> }
#> <bytecode: 0x7fc2aaa614d0>
#> <environment: 0x7fc2ad0daaa8>
#> 
#> [[3]]
#> function (x) 
#> {
#> }
#> <bytecode: 0x7fc2aaa614d0>
#> <environment: 0x7fc2aaa59ee8>

Created on 2018-03-09 by the reprex package (v0.2.0).

As this shows it produces a list a functions that return NULL no matter what is passed into them.
The prose description of what you want to do just isn’t clear enough for us to help you out.

A prose description isn’t sufficient, you also need to make a simple reprex that includes this:

  1. Builds the input data you are using.
  2. The function you are trying to write, even if it doesn’t work.
  3. Usage of the function you are trying to write, even if it doesn’t work.
  4. Builds the output data you want the function to produce.

Most of the people answering questions here are doing so in their spare time so anything you can to make it easier for them to help you is really appreciated.


#4

For future reference/ me: invoke_map was the desired function, right there on the old cheat sheet.

As for the responses I got: thanks.