![](/img/trans.png)
[英]How to refer to variable (column name) with tidyverse in a function?
[英]How do I pass a column name from a dataframe into a function using tidyverse syntax?
cars %>%
group_by(cyl) %>%
summarise_each(funs(mean(., na.rm = TRUE),
min(., na.rm = TRUE),
max(., na.rm = TRUE),
sd(., na.rm = TRUE)),
mpg, wt)
我想把上面的代碼變成一個function,其中dataframe(汽車)和列(cyl)是arguments。 如何在 R 中執行此操作?
我在下面嘗試了以下方法,但這沒有評估
plot_cars <- function (df, col) {
df %>%
group_by(col) %>%
summarise_each(funs(mean(., na.rm = TRUE),
min(., na.rm = TRUE),
max(., na.rm = TRUE),
sd(., na.rm = TRUE)),
mpg, wt)
}
plot_cars(cars,"cyl")
你可以試試這個:
plot_cars <- function (df,...) {
dots <- enquos(...)
df %>%
group_by(vars(!!!dots)) %>%
summarise_each(funs(mean(., na.rm = TRUE),
min(., na.rm = TRUE),
max(., na.rm = TRUE),
sd(., na.rm = TRUE)),
mpg, wt)
}
plot_cars(mtcars,cyl)
# A tibble: 1 x 9
`vars(cyl)` mpg_mean wt_mean mpg_min wt_min mpg_max wt_max mpg_sd wt_sd
<quos> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 cyl 20.1 3.22 10.4 1.51 33.9 5.42 6.03 0.978
這是使用惰性評估的類似解決方案。
plot_cars <- function (df, ...) {
df %>%
group_by_(.dots = lazyeval::lazy_dots(...)) %>%
summarise_each(funs(mean(., na.rm = TRUE),
min(., na.rm = TRUE),
max(., na.rm = TRUE),
sd(., na.rm = TRUE)),
mpg, wt)
plot_cars(mtcars, cyl)
}
如果您想了解更多相關信息,請點擊以下鏈接: https://medium.com/optima-blog/writing-your-own-dplyr-functions-a1568720db0d
foo = function(d, grp, ...) {
d %>%
group_by_at(grp) %>%
summarise_at(c(...), funs(mean(., na.rm = TRUE),
min(., na.rm = TRUE),
max(., na.rm = TRUE),
sd(., na.rm = TRUE)))
}
foo(mtcars, "cyl", "mpg", "wt")
如果我們需要傳遞一個字符串,請轉換為sym
並進行評估。 但是,為了更靈活,最好轉換為ensym
以便它可以同時使用 unquoted 和quoted
library(dplyr)#1.0.0
plot_cars <- function (df, col) {
col <- ensym(col)
df %>%
group_by(!!col) %>%
summarise(across(c(mpg, wt),
list(mean = ~ mean(., na.rm = TRUE),
min = ~ min(., na.rm = TRUE),
max = ~ max(., na.rm = TRUE),
sd = ~ sd(., na.rm = TRUE))))
}
plot_cars(mtcars,"cyl")
# A tibble: 3 x 9
# cyl mpg_mean mpg_min mpg_max mpg_sd wt_mean wt_min wt_max wt_sd
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 4 26.7 21.4 33.9 4.51 2.29 1.51 3.19 0.570
#2 6 19.7 17.8 21.4 1.45 3.12 2.62 3.46 0.356
#3 8 15.1 10.4 19.2 2.56 4.00 3.17 5.42 0.759
plot_cars(mtcars, cyl)
# A tibble: 3 x 9
# cyl mpg_mean mpg_min mpg_max mpg_sd wt_mean wt_min wt_max wt_sd
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 4 26.7 21.4 33.9 4.51 2.29 1.51 3.19 0.570
#2 6 19.7 17.8 21.4 1.45 3.12 2.62 3.46 0.356
#3 8 15.1 10.4 19.2 2.56 4.00 3.17 5.42 0.759
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.