在mutate_at()
包中使用dplyr
时收到警告消息。
dt %>%
mutate_at(
c(5:43),
funs(pc = ./Population)
)
Warning message:
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))
This warning is displayed once per session.
还有其他功能吗?
如何使用data.table
传递此密码。
答案 0 :(得分:3)
对于dplyr
的最新版本,它将是list
library(dplyr)
dt %>%
mutate_at(5:43,
list(pc = ~ ./Population))
可复制的示例
head(mtcars) %>%
mutate_at(4:5, list(pc = ~ ./wt))
# mpg cyl disp hp drat wt qsec vs am gear carb hp_pc drat_pc
#1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 41.98473 1.4885496
#2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 38.26087 1.3565217
#3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 40.08621 1.6594828
#4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 34.21462 0.9580093
#5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 50.87209 0.9156977
#6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 30.34682 0.7976879
警告消息是一种友好的警告,无需担心
在data.table
中,我们在.SDcols
中指定
library(data.table)
setDT(dt)[, paste0(names(dt)[5:43], "_pc") :=
lapply(.SD, function(x) x/Population), .SDcols = 5:43]
或使用base R
nm1 <- names(dt)[5:43]
dt[paste0(nm1, "_pc")] <- lapply(dt[nm1], `/`, dt[["Population"]])
或直接
dt[paste0(nm1, "_pc")] <- dt[nm1]/dt[["Population"]]