I have a dataframe that consists of functions I want to apply let f1 and f2 represent these functions that take dbh and ht as arguments.
spcd region function
122 'OR_W' f1
141 'OR_W' f2
I also have a dataframe that looks like
spcd region dbh ht
122 'OR_W' 12 101
122 'OR_W' 13 141
122 'OR_W' 15 122
141 'OR_W' 12 101
etc
I want to apply the functions stored in the first data frame to the rows in the second dataframe to produce something like this
spcd region dbh ht output
122 'OR_W' 12 101 <output of f1>
122 'OR_W' 13 141 <output of f1>
122 'OR_W' 15 122 <output of f1>
141 'OR_W' 12 101 <output of f2>
Where <output of f1>
is the output of the first function with the inputs of dbh and ht.
I think that dplyr's group_by would be useful for this, by grouping on spcd and region in the second dataframe, and then applying the correct function for each row in that group.
Is there a way to apply a function, row-wise, to a group within a dplyr group_by object?
Map(function(f,x)apply(x,1,f),c(data1$function),split(data2,spcd))
. If and only ifdata1$function
you can try usingis.function(data[1,3])
to see whether they are stored as functions or not - Onyambupurrr::invoke_map
variant, but you need to make the example reproducible to get a proper answer. - alistaire