This question is a "why", not a how. In the following code I'm trying to understand why dplyr::mutate evaluates one custom function (f()) with the entire vector but not with the other custom function (g()). What exactly is mutate doing?
set.seed(1);sum(rnorm(100, c(0, 10, 100))) f=function(m) { set.seed(1) sum(rnorm(100, mean=m)) } g <- function(m) sin(m) df <- data.frame(a=c(0, 10, 100)) y1 <- mutate(df, asq=a^2, fout=f(a), gout=g(a)) y2 <- rowwise(df) %>% mutate(asq=a^2, fout=f(a), gout=g(a)) y3 <- group_by(df, a) %>% summarize(asq=a^2, fout=f(a), gout=g(a)) For all three columns, asq, fout, and gout, evaluation is rowwise in y2 and y3 and the results are identical. However, y1$fout is 3640.889 for all three rows, which is the result of evaluating sum(rnorm(100, c(0, 10, 100))). So the function f() is evaluating the entire vector for each row.
A closely related question has been asked elsewhere mutate/transform in R dplyr (Pass custom function), but the "why" was not explained.