命名dplyr计数列

时间:2019-01-29 18:25:35

标签: r 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

帮助?

我正在跑步:

  • R版本3.5.0(2018-04-23)

  • 平台:i386-w64-mingw32 / i386(32位)

  • 在以下环境下运行:Windows> = 8 x64(内部版本9200)

0 个答案:

没有答案