【问题标题】:dplyr `across()` function requires `rowwise()` function yes?dplyr `across()` 函数需要 `rowwise()` 函数是吗?
【发布时间】:2020-05-04 14:50:53
【问题描述】:
packageVersion("dplyr")
#[1] ‘0.8.99.9002’

请注意,本题使用 dplyr 的新 across() 函数。要安装 dplyr 的最新开发版本,请发出 remotes::install_github("tidyverse/dplyr") 命令。要恢复到已发布的 dplyr 版本,请发出 install.packages("dplyr") 命令。如果您在未来某个时间阅读本文并且已经在 dplyr 1.X+ 上,则无需担心此说明。

library(tidyverse)
WorldPhones %>% 
  as.data.frame() %>% 
  rowwise() %>% 
  mutate(mean = mean(c_across(N.Amer:Mid.Amer), na.rm = TRUE))
#> # A tibble: 7 x 8
#> # Rowwise: 
#>   N.Amer Europe  Asia S.Amer Oceania Africa Mid.Amer   mean
#>    <dbl>  <dbl> <dbl>  <dbl>   <dbl>  <dbl>    <dbl>  <dbl>
#> 1  45939  21574  2876   1815    1646     89      555 10642 
#> 2  60423  29990  4708   2568    2366   1411      733 14600.
#> 3  64721  32510  5230   2695    2526   1546      773 15714.
#> 4  68484  35218  6662   2845    2691   1663      836 16914.
#> 5  71799  37598  6856   3000    2868   1769      911 17829.
#> 6  76036  40341  8220   3145    3054   1905     1008 19101.
#> 7  79831  43173  9053   3338    3224   2005     1076 20243.

This article by Dr Keith McNulty 提供了使用 dplyr 的新 c_across() 函数的一个很好的示例(如上所示)。您遍历每一行,然后 R 计算所选列之间的平均值。

让我们对 mtcars 数据框做同样的事情,而不是为每一行选择跨列的最大值。我们将只选择“drat”和“wt”变量以保持简单。

mtcars %>% 
  select(drat, wt) %>% 
  as_tibble() %>% 
  mutate(max = max(c_across(drat:wt), na.rm = TRUE))
#> # A tibble: 32 x 3
#>     drat    wt   max
#>    <dbl> <dbl> <dbl>
#>  1  3.9   2.62  5.42
#>  2  3.9   2.88  5.42
#>  3  3.85  2.32  5.42
#>  4  3.08  3.22  5.42
#>  5  3.15  3.44  5.42
#>  6  2.76  3.46  5.42
#>  7  3.21  3.57  5.42
#>  8  3.69  3.19  5.42
#>  9  3.92  3.15  5.42
#> 10  3.92  3.44  5.42
#> # ... with 22 more rows

为什么 dplyr 不选择每一行的最大值,并将其显示在 max 列中?我想要的应该是这样的。

#> # A tibble: 32 x 3
#>     drat    wt   max 
#>    <dbl> <dbl> <dbl>
#>  1  3.9   2.62   3.9
#>  2  3.9   2.88   3.9
#>  3  3.85  2.32  3.85
#>  4  3.08  3.22  3.22
#>  5  3.15  3.44  3.44
#>  6  2.76  3.46  3.46
#>  7  3.21  3.57  3.57
#>  8  3.69  3.19  3.69
#>  9  3.92  3.15  3.92
#> 10  3.92  3.44  3.92
#> # ... with 22 more rows

我该怎么做? c_across 在 worldphones 上工作,但不在 mtcars 上工作。我们将“工作”定义为“做我想做的事”。

【问题讨论】:

  • 为什么要从 mtcars 示例中删除 rowwise()
  • @Cpak 错过了原始文章中的 rowwise,盯着这个看了 30 分钟,真的看不到它。

标签: r dplyr max


【解决方案1】:

您没有rowwise 部分。请记住,c_across 仅对逐行操作有效(这是您现在想要的)。

mtcars %>% 
    select(drat, wt) %>% 
    rowwise() %>% 
    mutate(max = max(c_across(drat:wt), na.rm = TRUE))

# A tibble: 32 x 3
# Rowwise: 
    drat    wt   max
   <dbl> <dbl> <dbl>
 1  3.9   2.62  3.9 
 2  3.9   2.88  3.9 
 3  3.85  2.32  3.85
 4  3.08  3.22  3.22
 5  3.15  3.44  3.44
 6  2.76  3.46  3.46
 7  3.21  3.57  3.57
 8  3.69  3.19  3.69
 9  3.92  3.15  3.92
10  3.92  3.44  3.92
# ... with 22 more rows

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多