how to replace the now deprecated funs within rename_at

心不动则不痛 提交于 2021-01-28 19:54:11

问题


I'm trying (and succeeding) at renaming multiple columns in my dataframe using this code:

  rename_at(c("a", "b", "c"), 
            funs(paste0(., "_revenue")))

However, I then get this warning:

funs() is soft deprecated as of dplyr 0.8.0
Please use a list of either functions or lambdas: 

  # Simple named list: 
  list(mean = mean, median = median)

  # Auto named with `tibble::lst()`: 
  tibble::lst(mean, median)

  # Using lambdas
  list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))

I tried looking at https://dplyr.tidyverse.org/reference/select_all.html but I couldn't see any examples.

Please help.


回答1:


You just need to use list instead of funs and add lambda style.

# Before:
funs(name = f(.))

# After: 
list(name = ~ f(.))

For example:

> foo <- tibble(a = c(1, 2, 3), b = c(10, 20, 30), c = c(100, 200, 300))
> rename_at(foo, c("a", "b", "c"), list(~ paste0(., "_revenue")))
# A tibble: 3 x 3
  a_revenue b_revenue c_revenue
      <dbl>     <dbl>     <dbl>
1         1        10       100
2         2        20       200
3         3        30       300

There is a similar question here: How to change the now deprecated dplyr::funs() which includes an ifelse argument?



来源:https://stackoverflow.com/questions/57088113/how-to-replace-the-now-deprecated-funs-within-rename-at

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!