r - Variable name collision in dplyr -
i attempting compute moving median on variable contained within data.frame using dplyr. problem running function passing rollapply() has same name variable in original data.frame. example:
df <- data.frame(median = seq(1:100)) df %>% mutate(ln_median = log(median)) %>% mutate(ln_median_10 = rollapply(ln_median, 5, median))
generates error message:
error in eval(substitute(expr), envir, enclos) : '1:100' not function, character or symbol
the underlying cause median in rollapply() resolving variable in data.frame , not function "median". have been able around following code:
df %>% mutate(ln_median = log(median)) %>% mutate(ln_median_10 = rollapply(ln_median, 5, function(a) median(a), fill = na))
that is, wrapping median function in order suppress being interpreted variable within data.frame.
is there more elegant way of achieving same thing?
have tried pass function name in as
stats::median
Comments
Post a Comment