1

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?

Bryce Frank
  • 697
  • 10
  • 24
  • would you mind if you use base R? Also can you give a reproducible example? – Onyambu Dec 24 '17 at 03:44
  • `Map(function(f,x)apply(x,1,f),c(data1$function),split(data2,spcd))`. If and only if `data1$function` you can try using `is.function(data[1,3])` to see whether they are stored as functions or not – Onyambu Dec 24 '17 at 03:50
  • Join and use a `purrr::invoke_map` variant, but you need [to make the example reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610) to get a proper answer. – alistaire Dec 24 '17 at 05:03

1 Answers1

0

This is a base r solution;

 Map(apply,split(data1[-1],data1$d),1,c(data2$fun))) 

 data1["output"]=c(mapply(apply,split(data1[-1],data1$d),1,c(data2$fun)))
 data1
    d Girth Height Volume output
 1  1   8.3     70   10.3   88.6
 2  1   8.6     65   10.3   83.9
 3  1   8.8     63   10.2   82.0
 4  1  10.5     72   16.4   98.9
 5  1  10.7     81   18.8  110.5
 6  2  10.8     83   19.7   83.0
 7  2  11.0     66   15.6   66.0
 8  2  11.0     75   18.2   75.0
 9  2  11.1     80   22.6   80.0
 10 2  11.2     75   19.9   75.0

data used:

 data1=structure(list(d = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), 
                 Girth = c(8.3, 8.6, 8.8, 10.5, 10.7, 10.8, 11, 11, 11.1, 
                           11.2), Height = c(70, 65, 63, 72, 81, 83, 66, 75, 80, 75), 
                 Volume = c(10.3, 10.3, 10.2, 16.4, 18.8, 19.7, 15.6, 18.2, 
                            22.6, 19.9)), .Names = c("d", "Girth", "Height", "Volume"
                            ), row.names = c(NA, 10L), class = "data.frame")
 data2=structure(list(X1.2 = 1:2, fun = c("sum", "max")), .Names = c("X1.2", 
                                                                "fun"), row.names = c(NA, -2L), class = "data.frame")
Onyambu
  • 67,392
  • 3
  • 24
  • 53