I am trying to use paste inside of a dplyr's summarize or rename to name a variable when building a local function. I'm getting an Error: unexpected '=' in: " mutate(some.text= x) %>% rename(paste('hp', x, sep= '')="
Here is an example:
data(mtcars)
get.information <- function(df, x) {
df %>%
group_by(hp) %>%
summarize(paste('hp', x, sep= "")= n()) %>%
mutate(some.text= x)
}
or
get.information <- function(df, x) {
df %>%
group_by(hp) %>%
summarize(Frequency= n()) %>%
mutate(some.text= x) %>%
rename(paste('hp', x, sep= "")= Frequency)
}
get.information(mtcars, 2)
I'd appreciate any help, thanks in advance!
Try this:
get.information <- function(df, x) {
var.name = paste0('hp', x)
var.val = lazyeval::interp('Frequency')
df %>%
group_by(hp) %>%
summarize(Frequency= n()) %>%
mutate(some.text= x) %>%
mutate_(.dots = setNames(list(var.val), var.name))
}