在没有波形符或引号的情况下输入dplyr中的点

时间:2018-01-19 15:32:17

标签: r dplyr

如何让以下代码不带引号或代字号?

这对我很重要,因为我在自定义函数中使用这个独立代码,引入引号或代码使事情变得复杂。

感谢。

dataframe <- as.data.frame(cbind(gender = c("male", "female", "female", "female", "male", "female", "male"),
                                 pet = c("dog", "cat", "dog", "cat", "cat", "cat", "dog")))

library(dplyr)

## with tilde

dplyr::group_by_(dataframe, .dots = c(~gender, ~pet)) %>%
  dplyr::summarize(counts = n()) %>%
  dplyr::mutate(perc = (counts / sum(counts)) * 100) %>%
  dplyr::arrange(desc(perc))
#> # A tibble: 4 x 4
#> # Groups: gender [2]
#>   gender pet   counts  perc
#>   <fct>  <fct>  <int> <dbl>
#> 1 female cat        3  75.0
#> 2 male   dog        2  66.7
#> 3 male   cat        1  33.3
#> 4 female dog        1  25.0

## with quotes

dplyr::group_by_(dataframe, .dots = c('gender', 'pet')) %>%
  dplyr::summarize(counts = n()) %>%
  dplyr::mutate(perc = (counts / sum(counts)) * 100) %>%
  dplyr::arrange(desc(perc))
#> # A tibble: 4 x 4
#> # Groups: gender [2]
#>   gender pet   counts  perc
#>   <fct>  <fct>  <int> <dbl>
#> 1 female cat        3  75.0
#> 2 male   dog        2  66.7
#> 3 male   cat        1  33.3
#> 4 female dog        1  25.0

## without tilde

dplyr::group_by_(dataframe, .dots = c(gender, pet)) %>%
  dplyr::summarize(counts = n()) %>%
  dplyr::mutate(perc = (counts / sum(counts)) * 100) %>%
  dplyr::arrange(desc(perc))
#> Error in compat_lazy_dots(.dots, caller_env(), ...): object 'gender' not found

3 个答案:

答案 0 :(得分:2)

如果我们需要在函数中使用它,那么将组作为一个quosure传递并使用!!!进行评估

f1 <- function(dat, groups) {
 dat %>%
    group_by(!!! groups) %>%
    summarize(counts = n()) %>%
    mutate(perc = (counts / sum(counts)) * 100) %>% 
    arrange(desc(perc))
 }

f1(dataframe, quos(gender, pet))
# A tibble: 4 x 4
# Groups: gender [2]
#  gender pet    counts  perc
#  <fctr> <fctr>  <int> <dbl>
#1 female cat         3  75.0
#2 male   dog         2  66.7
#3 male   cat         1  33.3
#4 female dog         1  25.0

除上述内容外,我们可以将...作为群组的参数,将其转换为quo_name的字符串,并将其传递到group_by_at

f2 <- function(dat, ...) {
  groups <-  rlang::enquos(...) %>%
                    purrr::map_chr(quo_name)

 dat %>%
     group_by_at(vars(groups)) %>%
     summarize(counts = n()) %>%
     mutate(perc = (counts / sum(counts)) * 100) %>% 
    arrange(desc(perc))
}

f2(dataframe, gender, pet)

答案 1 :(得分:2)

改为使用group_by

dplyr::group_by(dataframe, gender, pet) %>%
  dplyr::summarize(counts = n()) %>%
  dplyr::mutate(perc = (counts / sum(counts)) * 100) %>%
  dplyr::arrange(desc(perc))

#The result:
# Groups: gender [2]
#  gender pet    counts  perc
#  <fctr> <fctr>  <int> <dbl>
#1 female cat         3  75.0
#2 male   dog         2  66.7
#3 male   cat         1  33.3
#4 female dog         1  25.0

答案 2 :(得分:2)

使用dplyr编程进行多重分组的两种方式: -
Way1

f1<-function(df,...){
 grp_vars<-quos(...) 

 df %>% 
 group_by(!!!grp_vars) %>%  
   summarize(counts= n()) %>% 
   mutate(perc = (counts/sum(counts))*100) %>% 
   arrange(desc(perc))
}

f1(dataframe, gender, pet)

结果1:

 # A tibble: 4 x 4
    # Groups:   gender [2]
      gender pet   counts  perc
      <fct>  <fct>  <int> <dbl>
    1 female cat        3  75.0
    2 male   dog        2  66.7
    3 male   cat        1  33.3
    4 female dog        1  25.0

Way2:

f2<-function(df, grp_vars){
  df %>% 
    group_by(!!!grp_vars) %>% 
    summarize(counts= n()) %>% 
    mutate(perc = (counts/sum(counts))*100) %>% 
    arrange(desc(perc))
}
f2(dataframe, quos(gender,pet))

结果2:

# A tibble: 4 x 4
# Groups:   gender [2]
  gender pet   counts  perc
  <fct>  <fct>  <int> <dbl>
1 female cat        3  75.0
2 male   dog        2  66.7
3 male   cat        1  33.3
4 female dog        1  25.0