I have a dataframe, where different lines require different evaluations to compute a result. Each of these evaluations is implemented in a function, and the respective function to use is specified in a column in the dataframe. Here is a minimal example:
f1 = function(a,...){return(2*a)}
f2 = function(a,b,...){return(a+b)}
df = data.frame(a=1:4,b=5:8,f=c('f1','f2','f2','f1'))
#Expected result:
a b f result
1 1 5 f1 2
2 2 6 f2 8
3 3 7 f2 10
4 4 8 f1 8
With pmap, I am able to apply a function to each row of a dataframe, and I also read about exec() replacing invoke_map(), but none of my attempts to combine both seem to work because exec() only seems to work with lists:
df$result = purrr::pmap(df,df$f)
df$result = purrr::pmap(df$f,exec,df)
...
Is there a more elegant way than filtering the dataframe for each function, using pmap on each filtered dataframe and then binding everything back together?
Thank you in advance!
Edit: I should mention that my dataframe has a lot of columns, and that the functions do not need the same arguments (e.g. some may be skipping ´´´a´´´, but require ´´´b´´´). Therefore I need a method where I don't need to pass the arguments explicitly.