열 이름을 입력으로 사용하는 함수가 있습니다.
library(tidyverse)
dat <- diamonds
ex_func <- function(df, grp) {
df %>%
distinct(cut, get(grp))
}
ex_func(dat, grp = "color")
결과 그룹에서 두 번째 열의 이름을 입력으로 정확히 어떻게 얻습니까 (예 : get (grp)가 아닌 "color")?
인용되지 않거나 인용 된 값을 모두 사용하는 경우 ensym
( !!
) 를 사용 하고 평가 하십시오.
library(dplyr)
ex_func <- function(df, grp) {
df %>%
distinct(cut, !! rlang::ensym(grp))
}
-테스트
ex_func(dat, "color")
# A tibble: 35 x 2
# cut color
# <ord> <ord>
# 1 Ideal E
# 2 Premium E
# 3 Good E
# 4 Premium I
# 5 Good J
# 6 Very Good J
# 7 Very Good I
# 8 Very Good H
# 9 Fair E
#10 Ideal J
# … with 25 more rows
ex_func(dat, color)
# A tibble: 35 x 2
# cut color
# <ord> <ord>
# 1 Ideal E
# 2 Premium E
# 3 Good E
# 4 Premium I
# 5 Good J
# 6 Very Good J
# 7 Very Good I
# 8 Very Good H
# 9 Fair E
#10 Ideal J
# … with 25 more rows
따옴표없이 만 사용하려는 경우 옵션은 다음과 같습니다. {{}}
ex_func <- function(df, grp) {
df %>%
distinct(cut, {{grp}})
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다