复制dplyr quote和enquote示例

复制dplyr quote和enquote示例,r,dplyr,tidyr,tidyverse,R,Dplyr,Tidyr,Tidyverse,我很难复制[dplyr.tidyverse.org]中的示例。 这是我的代码和会话信息。今天我花了太多的时间去寻找虚假线索,重新安装软件包。非常感谢您的帮助 library(dplyr) df <- tibble( g1 = c(1, 1, 2, 2, 2), g2 = c(1, 2, 1, 2, 1), a = sample(5), b = sample(5) ) my_summarise <- function(df, group_by) { group

我很难复制[dplyr.tidyverse.org]中的示例。
这是我的代码和会话信息。今天我花了太多的时间去寻找虚假线索,重新安装软件包。非常感谢您的帮助

library(dplyr)  

 df <- tibble(  
 g1 = c(1, 1, 2, 2, 2),
 g2 = c(1, 2, 1, 2, 1),
 a = sample(5), 
 b = sample(5)
)

my_summarise <- function(df, group_by) {
 group_by <- enquo(group_by)
 print(group_by)

 df %>%
  group_by_(!!group_by) %>%
  summarise(a = mean(a))
}

my_summarise(df, g1)  
以下是会话信息:

R version 3.4.0 (2017-04-21)  
Platform: x86_64-w64-mingw32/x64 (64-bit)  
Running under: Windows 7 x64 (build 7601) Service Pack 1  


Matrix products: default  

locale:  
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          

[5] LC_TIME=English_United States.1252    

attached base packages:  
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.7.4.9000     lubridate_1.5.6      pander_0.6.0         
RevoUtilsMath_10.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.11     digest_0.6.9     withr_1.0.2      assertthat_0.2.0 
R6_2.2.0         magrittr_1.5    
 [7] git2r_0.18.0     httr_1.2.1       stringi_1.1.5    rlang_0.1.2.9000 
curl_2.6         bindrcpp_0.2    
[13] devtools_1.12.0  RevoUtils_10.0.4 tools_3.4.0      stringr_1.2.0    
glue_1.1.1       compiler_3.4.0    
[19] pkgconfig_2.0.1  memoise_1.1.0    bindr_0.1        knitr_1.16       
tibble_1.3.3     

以下内容适合我<代码>分组依据已被弃用,因此请使用
分组依据
。顺便说一下,您可以使用
groupby
作为函数的参数名。我认为这是一个坏主意,因为
groupby
dplyr
中定义的函数。您可能需要考虑更改此参数名以避免混淆。
library(dplyr)  

df <- tibble(  
  g1 = c(1, 1, 2, 2, 2),
  g2 = c(1, 2, 1, 2, 1),
  a = sample(5), 
  b = sample(5)
)

my_summarise <- function(df, group_by) {
  group_by <- enquo(group_by)
  print(group_by)

  df %>%
    group_by(!!group_by) %>%
    summarise(a = mean(a))
}

my_summarise(df, g1) 
<quosure: global>
~g1
# A tibble: 2 x 2
     g1     a
  <dbl> <dbl>
1     1     3
2     2     3
库(dplyr)

df以下内容对我有用<代码>分组依据
已被弃用,因此请使用
分组依据
。顺便说一下,您可以使用
groupby
作为函数的参数名。我认为这是一个坏主意,因为
groupby
dplyr
中定义的函数。您可能需要考虑更改此参数名以避免混淆。
library(dplyr)  

df <- tibble(  
  g1 = c(1, 1, 2, 2, 2),
  g2 = c(1, 2, 1, 2, 1),
  a = sample(5), 
  b = sample(5)
)

my_summarise <- function(df, group_by) {
  group_by <- enquo(group_by)
  print(group_by)

  df %>%
    group_by(!!group_by) %>%
    summarise(a = mean(a))
}

my_summarise(df, g1) 
<quosure: global>
~g1
# A tibble: 2 x 2
     g1     a
  <dbl> <dbl>
1     1     3
2     2     3
库(dplyr)

df我遇到了完全相同的问题,上面发布的代码ycw给出了
无效的参数类型
。它似乎只出现在我升级dplyr的会话中,一旦我开始一个新的会话,代码ycw发布的工作正常。

我遇到了完全相同的问题,上面发布的代码ycw给出了
无效的参数类型。它似乎只出现在我升级dplyr的会话中,一旦我启动了一个新会话,ycw发布的代码就可以正常工作