簡體   English   中英

使用傳遞列參數創建dplyr函數

[英]Creating dplyr function with passing column argument

我試圖將列名作為參數傳遞給使用dplyr函數的函數。

圍繞這個主題已經有多個問題,我嘗試了所有這些問題,一切似乎都拋出了一些或其他錯誤。

我用了enquo !! 如此處所示 嘗試使用!! as_label用來對抗我上一步使用得到的錯誤。 也試過提到改用GROUP_BY的group_by_ 這里 我也嘗試過卷曲運算符來解決

userMaster <- structure(list(user_id = c(1, 2, 3, 4, 5), city = structure(c(5L, 
5L, 8L, 9L, 10L), .Label = c("Austin", "Boise", "Boston", "Chicago", 
"Dallas", "Denver", "Detroit", "Kansas City", "Las Vegas", "Los Angeles", 
"Manhattan", "Miami", "Minneapolis", "New York City", "Oklahoma City", 
"Omaha", "Phoenix", "Saint Louis", "San Francisco", "Washington DC"
), class = "factor"), source = structure(c(2L, 2L, 2L, 2L, 2L
), .Label = c("Adwords", "Organic", "Search Ads"), class = "factor")), row.names = c(NA, 
5L), class = "data.frame")

userCount <- function(table, metric){
  col_enquo <- enquo(metric)

  summary <- table %>% select(!! (col_enquo), source, user_id) %>%
    group_by_(!! (col_enquo), source) %>% summarise(users = n_distinct(user_id)) %>% 
    left_join(table %>% group_by(source) %>% 
                summarise(total = n_distinct(user_id))) %>% mutate(users/total)
  return(summary)
}

genderDemo <- userCount(userMaster, city)

我得到了各種類型的錯誤 -

Error: `quos(desire)` must evaluate to column positions or names, not a list

Error in !as_label(col_enquo) : invalid argument type 

Error: Quosures can only be unquoted within a quasiquotation context.

  # Bad:
  list(!!myquosure)

  # Good:
  dplyr::mutate(data, !!myquosure)

使用rlang_0.4.0 ,我們可以使用{{...}} (卷曲 - 卷曲運算符),它可以使評估更簡單

library(rlang) #v 0.4.0
library(dplyr) #v 0.8.3
userCount <- function(tbl, metric){

  tbl %>% 
       select({{metric}}, source, user_id) %>%
       group_by({{metric}}, source) %>% 
       summarise(users = n_distinct(user_id)) %>% 
       left_join(tbl %>% 
                group_by(source) %>% 
                summarise(total = n_distinct(user_id))) %>% 
                 mutate(users/total)

   }

genderDemo <- userCount(userMaster, desire)
genderDemo
# A tibble: 12 x 5
# Groups:   desire [4]
#   desire source users total `users/total`
#   <fct>  <fct>  <int> <int>         <dbl>
# 1 A      a          2     4         0.5  
# 2 A      b          1     3         0.333
# 3 A      c          2     5         0.4  
# 4 B      a          1     4         0.25 
# 5 B      b          1     3         0.333
# 6 B      c          1     5         0.2  
# 7 C      a          1     4         0.25 
# 8 C      b          2     3         0.667
# 9 C      c          1     5         0.2  
#10 D      a          1     4         0.25 
#11 D      b          1     3         0.333
#12 D      c          2     5         0.4  

使用OP的數據

userCount(userMaster2, city)
#Joining, by = "source"
# A tibble: 4 x 5
# Groups:   city [4]
#  city        source  users total `users/total`
#  <fct>       <fct>   <int> <int>         <dbl>
#1 Dallas      Organic     2     5           0.4
#2 Kansas City Organic     1     5           0.2
#3 Las Vegas   Organic     1     5           0.2
#4 Los Angeles Organic     1     5           0.2

注意: -后綴方法已被棄用。 因此,要么在group_by使用{{..}}要么在group_by使用{{..}} group_by(!! enquo(col_enquo))

數據

set.seed(24)
userMaster <- data.frame(desire = rep(LETTERS[1:4], each = 5),
                        user_id = sample(1:5, 20, replace = TRUE),
                        source = sample(letters[1:3], 20, replace = TRUE))  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM