【问题标题】:summarise() in dplyr 1.0.2 acting like mutate()dplyr 1.0.2 中的 summarise() 作用类似于 mutate()
【发布时间】:2020-10-23 18:29:21
【问题描述】:

给定一个列出用户、产品和产品功能的tibble,我正在尝试计算具有特定产品功能的不同产品用户的比例:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

df <- tribble(
  ~users, ~product, ~feature,
  "bob","iPhone","screen",
  "bob","iPhone","camera",
  "bob","iPhone","facial recognition",
  "sally","Android","screen",
  "sally","Android","camera",
  "sally","Android","facial recognition",
  "joe","Huawei","screen",
  "joe","Huawei","camera",
  "joe","Huawei","facial recognition",
  "rachel","iPhone","screen",
  "rachel","iPhone","camera",
  "rachel","iPhone","fingerprint sensor"
)

# Get count of distinct users by product
df <- df %>%
  group_by(product) %>%
  mutate(n_users = n_distinct(users)) %>%
  ungroup()

df
#> # A tibble: 12 x 4
#>    users  product feature            n_users
#>    <chr>  <chr>   <chr>                <int>
#>  1 bob    iPhone  screen                   2
#>  2 bob    iPhone  camera                   2
#>  3 bob    iPhone  facial recognition       2
#>  4 sally  Android screen                   1
#>  5 sally  Android camera                   1
#>  6 sally  Android facial recognition       1
#>  7 joe    Huawei  screen                   1
#>  8 joe    Huawei  camera                   1
#>  9 joe    Huawei  facial recognition       1
#> 10 rachel iPhone  screen                   2
#> 11 rachel iPhone  camera                   2
#> 12 rachel iPhone  fingerprint sensor       2

# Count the fraction of distinct users with given product feature
df <- df %>%
  group_by(product, feature) %>%
  summarise(feature_fraction = n()/n_users,
            .groups = "drop_last")

df
#> # A tibble: 12 x 3
#> # Groups:   product [3]
#>    product feature            feature_fraction
#>    <chr>   <chr>                         <dbl>
#>  1 Android camera                          1  
#>  2 Android facial recognition              1  
#>  3 Android screen                          1  
#>  4 Huawei  camera                          1  
#>  5 Huawei  facial recognition              1  
#>  6 Huawei  screen                          1  
#>  7 iPhone  camera                          1  
#>  8 iPhone  camera                          1  
#>  9 iPhone  facial recognition              0.5
#> 10 iPhone  fingerprint sensor              0.5
#> 11 iPhone  screen                          1  
#> 12 iPhone  screen                          1
Created on 2020-10-23 by the reprex package (v0.3.0)

devtools::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  ctype    English_United States.1252  
#>  tz       America/New_York            
#>  date     2020-10-23                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.2)
#>  backports     1.1.10  2020-09-15 [1] CRAN (R 4.0.2)
#>  callr         3.4.4   2020-09-07 [1] CRAN (R 4.0.2)
#>  cli           2.0.2   2020-02-28 [1] CRAN (R 4.0.2)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.2)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 4.0.2)
#>  devtools      2.3.1   2020-07-21 [1] CRAN (R 4.0.2)
#>  digest        0.6.25  2020-02-23 [1] CRAN (R 4.0.2)
#>  dplyr       * 1.0.2   2020-08-18 [1] CRAN (R 4.0.2)
#>  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.2)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.2)
#>  fansi         0.4.1   2020-01-08 [1] CRAN (R 4.0.2)
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)
#>  generics      0.0.2   2018-11-29 [1] CRAN (R 4.0.2)
#>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
#>  highr         0.8     2019-03-20 [1] CRAN (R 4.0.2)
#>  htmltools     0.5.0   2020-06-16 [1] CRAN (R 4.0.2)
#>  knitr         1.29    2020-06-23 [1] CRAN (R 4.0.2)
#>  lifecycle     0.2.0   2020-03-06 [1] CRAN (R 4.0.2)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 4.0.2)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 4.0.2)
#>  pillar        1.4.6   2020-07-10 [1] CRAN (R 4.0.2)
#>  pkgbuild      1.1.0   2020-07-13 [1] CRAN (R 4.0.2)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.2)
#>  pkgload       1.1.0   2020-05-29 [1] CRAN (R 4.0.2)
#>  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.2)
#>  processx      3.4.4   2020-09-03 [1] CRAN (R 4.0.2)
#>  ps            1.3.4   2020-08-11 [1] CRAN (R 4.0.2)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.2)
#>  R6            2.4.1   2019-11-12 [1] CRAN (R 4.0.2)
#>  remotes       2.2.0   2020-07-21 [1] CRAN (R 4.0.2)
#>  rlang         0.4.7   2020-07-09 [1] CRAN (R 4.0.2)
#>  rmarkdown     2.3     2020-06-18 [1] CRAN (R 4.0.2)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 4.0.2)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.2)
#>  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.2)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.2)
#>  testthat      2.3.2   2020-03-02 [1] CRAN (R 4.0.2)
#>  tibble        3.0.3   2020-07-10 [1] CRAN (R 4.0.2)
#>  tidyselect    1.1.0   2020-05-11 [1] CRAN (R 4.0.2)
#>  usethis       1.6.1   2020-04-29 [1] CRAN (R 4.0.2)
#>  utf8          1.1.4   2018-05-24 [1] CRAN (R 4.0.2)
#>  vctrs         0.3.4   2020-08-29 [1] CRAN (R 4.0.2)
#>  withr         2.3.0   2020-09-22 [1] CRAN (R 4.0.2)
#>  xfun          0.16    2020-07-24 [1] CRAN (R 4.0.2)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.2)

可以看出,最终的tibble 有多行用于具有相同汇总值的组键对。据我所知,这是summarise 的意外行为,似乎与mutate 将返回的行为几乎相同。鉴于this open github issue,似乎所有问题都没有从summarise 的新版本中解决。我也可能只是愚蠢,如果有人能帮助我重回正轨,我将不胜感激!

【问题讨论】:

    标签: r dplyr tidyverse


    【解决方案1】:

    问题是每个组的n_users 有多个值。如果您的摘要函数返回多个值,最新版本的dplyr 允许您在每个组中返回多于一行。

    如果您想假设n_users 的所有值在每个组中都相同,那么您可以这样做

    df %>%
      group_by(product, feature) %>%
      summarise(feature_fraction = n()/first(n_users),
                .groups = "drop_last")
    

    这将确保每个组只返回一个值

    【讨论】:

    • 不是听起来粗鲁的意思,但是如果您查看我的代码,那正是我所拥有的。此外,在这种情况下,汇总函数不会返回组键对的多个值,也不会汇总到单行。
    • 你有n()/n_users,而不是n()/first(n_users)。那些是不同的。在第一种情况下,n_users 可能有多个值。 first() 将确保只返回 1 个值。如果您认为它们相同,您也可以使用unique()。旧版本的 dplyr 可能会引发错误。
    • @DanielMolitor 在说出任何听起来很粗鲁的话之前,您应该尝试代码并仔细查看。 1 / c(2, 2, 2) 返回 c(.5, .5, .5) 这(基本上)就是您的代码所具有的,因为每一行都有一个 n_user 值。此答案将其更改为1 / first(c(2, 2, 2) = 1 / 2 = 0.5。如果您尝试此答案,您会发现它解决了您的问题,并且解释也准确。
    • 好点,谢谢!但是,鉴于在我的情况下,n_users 的值对于整个组都是相同的,我不明白为什么 dplyr 1.0.0 之前的行为会改变。这似乎可能会引起混乱(至少对我来说是这样)。
    • 是的 - 我同意这令人困惑。它为summarize 增加了很多功能,但我喜欢干净的sumarize -> 1 row per group 界面,我希望这是一个具有新名称的新功能。 RStudio 社区论坛或dplyr GitHub 存储库将是讨论设计选择的地方。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    相关资源
    最近更新 更多