dplyr:如何在`bind_rows`中取消引用?

时间:2017-11-10 19:54:55

标签: r dplyr nse

我对dplyr的惊人新体验是,如果我对涉及非引号(tibble!!x)的rlang::UQ(x)进行各种操作,则将它们分配给变量并最终合并他们bind_rows一切都很好;嵌入bind_rows的相同操作失败并出现一些错误。更令人困惑的是,错误并不总是一样:我得到quote(x) must resolve to integer column positions, not formula(这是selectmutate)和Column quote(x) is of unsupported type quoted call(这与group_by)。我想知道这种行为背后是什么,我做错了什么或者这是一个错误?

require(dplyr)

f1 <- function(d, var){

    qvar <- enquo(var)

    d1 <- d %>% group_by(!!qvar) %>% summarize_all(first)
    d2 <- d %>% group_by(!!qvar) %>% summarize_all(first)

    bind_rows(d1, d2)

}

f2 <- function(d, var){

    qvar <- enquo(var)

    bind_rows(
        d %>%  group_by(!!qvar) %>% summarize_all(first),
        d %>%  group_by(!!qvar) %>% summarize_all(first)
    )
}

d <- data.frame(
    x = c(rep('a', 3), rep('b', 3)),
    y = seq(6)
)

f1(d, x) # works

f2(d, x) # fails

0 个答案:

没有答案