I have a data frame. For an argument, let's say this is a data frame datasets::women. I want to create a vector from a frame by applying a function to each row.
It seems that the usual way to do this is to use dplyrand call, mutateor transmute, for example:
dplyr::transmute(women, some_index = 2 * height + weight)
Excellent: it works. But what if I pulled the calculation some_indexinto a function acting on a string:
calc_some_index <- function(woman) {
2 * woman$height + woman$weight
}
Is there a way I should call mutate/ transmuteso that it calls this function on every line of its input?
Of course, I see that I get the correct result if I call
dplyr::transmute(women, some_index=calc_some_index(women))
, "", , , transmute. , , :
dplyr::transmute(head(women, n=10), some_index=calc_some_index(women))