title: map function based on a column value of dataframes stored in a list
body: <p>I am trying to use the map function to do something complex - I'd like to use the values of the Result column  per each dataframe I have in a list ( these are monthly dataframes and should be kept separated) and iterate this column with an external vector which should change according to a categorical variable inside the dataframe. Thus I defined two different functions to be passed inside map but I am getting an error. The ideal would be also to create a new column in each dataframe of the list to store the new values .. but I am not sure how to do that with &quot;mutate&quot; given that the object is a list.</p>
  <p>Thanks a lot</p>
  <pre class="lang-r prettyprint-override"><code>rm(list = ls())
  
  setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
  #&gt; Error: RStudio not running
  getwd()
  #&gt; [1] &quot;C:/Users/Angela/AppData/Local/Temp/RtmpYhghZ1/reprex-3a1458d04eec-pure-tayra&quot;
  
  #load required packages 
  library(mc2d)
  #&gt; Loading required package: mvtnorm
  #&gt; 
  #&gt; Attaching package: 'mc2d'
  #&gt; The following objects are masked from 'package:base':
  #&gt; 
  #&gt;     pmax, pmin
  library(gplots)
  #&gt; 
  #&gt; Attaching package: 'gplots'
  #&gt; The following object is masked from 'package:stats':
  #&gt; 
  #&gt;     lowess
  library(RColorBrewer)
  library(dplyr)
  #&gt; 
  #&gt; Attaching package: 'dplyr'
  #&gt; The following objects are masked from 'package:stats':
  #&gt; 
  #&gt;     filter, lag
  #&gt; The following objects are masked from 'package:base':
  #&gt; 
  #&gt;     intersect, setdiff, setequal, union
  library(reprex)
  library(tidyverse)
  set.seed(99)
  iters&lt;-1000
  
  df&lt;-data.frame(id=c(1:30),cat=c(rep(&quot;a&quot;,12),rep(&quot;b&quot;,18)),month=c(1:6,1,6,4,1,5,2,3,2,5,4,6,3:6,4:6,1:5,5),n=rpois(30,5))
  
  df$n[df$n == &quot;0&quot;] &lt;- 3
  se&lt;-rbeta(iters,96,6)
  epi.a&lt;-rpert(iters,min=1.5, mode=2, max=3)
  p=0.2
  p2=epi.a*p
  
  df&lt;-as_tibble(df)
  # this defined function ensures any `n` from `df` will be itered with 10000 s and a and generated 10000 results
  iter_n &lt;- function(n) map2_dbl(.x = se, .y = p2, ~ 1 - (1 - .x * .y) ^ n)
  list_1 &lt;- df %&gt;% mutate(Result = map(n, ~iter_n(.x))) %&gt;% unnest(Result)%&gt;% group_split(month)
  list_1[[1]]
  #&gt; # A tibble: 4,000 x 5
  #&gt;       id cat   month     n Result
  #&gt;    &lt;int&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;
  #&gt;  1     1 a         1     5  0.953
  #&gt;  2     1 a         1     5  0.927
  #&gt;  3     1 a         1     5  0.904
  #&gt;  4     1 a         1     5  0.945
  #&gt;  5     1 a         1     5  0.872
  #&gt;  6     1 a         1     5  0.840
  #&gt;  7     1 a         1     5  0.896
  #&gt;  8     1 a         1     5  0.944
  #&gt;  9     1 a         1     5  0.925
  #&gt; 10     1 a         1     5  0.937
  #&gt; # ... with 3,990 more rows
  
  p3a=rbeta(iters,50,5)
  p3b=rbeta(iters,40,6)
  
  iter_n2a&lt;-function(Result) map_dbl(p3a, ~ prod(1 - Result * .x))
  iter_n2b&lt;-function(Result) map_dbl(p3b, ~ prod(1 - Result * .x))
  
  list_2 &lt;- list_1%&gt;% map( ~ mutate(n_p = if_else(.x$cat == &quot;a&quot;,
                                      map(.x$Result,  ~ iter_n2a(.x)),
                                      map(.x$Result,  ~ iter_n2b(.x)))))
  #&gt; Error in UseMethod(&quot;mutate&quot;): no applicable method for 'mutate' applied to an object of class &quot;list&quot;
  </code></pre>
  <p><sup>Created on 2022-05-06 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v2.0.1)</sup></p>