Consider the following data:
df <- data.frame(names = sample(letters[1:5], 20, replace = T), numbers = 1:20)
for which we have the function cumsum
, such that for each row in a group, it computes the cumulative sum of all numbers up to that row
library(dplyr)
df %>%
group_by(names) %>%
mutate(cumsum_numbers = cumsum(numbers))
I wish to apply a general function my_fn
cumulatively in the same manner as cumsum
. my_fn
has general form:
my_fn <- function(vector){
# do stuff to vector
return(x) # x is a numeric scalar
}
that is, it takes a vector of previous values to the row, and returns a scalar.
The following code does not work:
df %>%
group_by(names) %>%
mutate(cumsum_numbers = my_fn(numbers)) # will apply my_fn
# to each group in numbers, returning the
# same value for each grouping level
So I guess I want something like:
df %>%
group_by(names) %>%
mutate(cumsum_numbers = cum_my_fn(numbers))
Note that an example function would be mean
for calculating the cumulative mean. Interestingly dplyr
has implemented cummean
, but I don't know the internal workings of this so I can't work out how to implement this behaviour for a general function.