首页 > 解决方案 > 命名 dplyr 计数列

问题描述

根据https://dplyr.tidyverse.org/reference/tally.html,运行以下代码:

# Change the name of the newly created column:
species <-
 starwars %>%
 count(species, homeworld, sort = TRUE, name = "n_species_by_homeworld")
species

应该产生:

#> # A tibble: 58 x 3
#>    species homeworld n_species_by_homeworld
#>    <chr>   <chr>                      <int>
#>  1 Human   Tatooine                       8
#>  2 Human   <NA>                           5
#>  3 Human   Naboo                          5
#>  4 Gungan  Naboo                          3
#>  5 Human   Alderaan                       3
#>  6 <NA>    <NA>                           2
#>  7 <NA>    Naboo                          2
#>  8 Droid   <NA>                           2
#>  9 Droid   Tatooine                       2
#> 10 Human   Corellia                       2
#> # … with 48 more rows

但是,我得到:

# A tibble: 58 x 4
   species  homeworld name                       n
   <chr>    <chr>     <chr>                  <int>
 1 Human    Tatooine  n_species_by_homeworld     8
 2 Human    Naboo     n_species_by_homeworld     5
 3 Human    NA        n_species_by_homeworld     5
 4 Gungan   Naboo     n_species_by_homeworld     3
 5 Human    Alderaan  n_species_by_homeworld     3
 6 Droid    Tatooine  n_species_by_homeworld     2
 7 Droid    NA        n_species_by_homeworld     2
 8 Human    Corellia  n_species_by_homeworld     2
 9 Human    Coruscant n_species_by_homeworld     2
10 Kaminoan Kamino    n_species_by_homeworld     2
# ... with 48 more rows

帮助?

我在跑:

标签: rdplyr

解决方案


推荐阅读