Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

across() not operating sequentially #5773

Merged
merged 3 commits into from
Feb 18, 2021
Merged

Conversation

romainfrancois
Copy link
Member

I added some tests too, code from #5770:

library(palmerpenguins); library(tidyverse)
(tbl1 <- penguins %>%
    group_by(species, island, sex) %>%
    summarize(n = n()) %>%
    pivot_wider(names_from = sex, values_from = n))
#> `summarise()` has grouped output by 'species', 'island'. You can override using the `.groups` argument.
#> # A tibble: 5 x 5
#> # Groups:   species, island [5]
#>   species   island    female  male  `NA`
#>   <fct>     <fct>      <int> <int> <int>
#> 1 Adelie    Biscoe        22    22    NA
#> 2 Adelie    Dream         27    28     1
#> 3 Adelie    Torgersen     24    23     5
#> 4 Chinstrap Dream         34    34    NA
#> 5 Gentoo    Biscoe        58    61     5
(tbl2 <- tbl1 %>%
    ungroup() %>%
    rowwise(species, island) %>%
    mutate(across(everything(), ~ . / sum(c_across(everything()), na.rm = T))))
#> # A tibble: 5 x 5
#> # Rowwise:  species, island
#>   species   island    female  male    `NA`
#>   <fct>     <fct>      <dbl> <dbl>   <dbl>
#> 1 Adelie    Biscoe     0.5   0.5   NA     
#> 2 Adelie    Dream      0.482 0.5    0.0179
#> 3 Adelie    Torgersen  0.462 0.442  0.0962
#> 4 Chinstrap Dream      0.5   0.5   NA     
#> 5 Gentoo    Biscoe     0.468 0.492  0.0403
(tbl3 <- tbl1 %>%
    ungroup() %>%
    rowwise(species, island) %>%
    mutate(across(female:male, ~ . / sum(c_across(female:male), na.rm = T))))
#> # A tibble: 5 x 5
#> # Rowwise:  species, island
#>   species   island    female  male  `NA`
#>   <fct>     <fct>      <dbl> <dbl> <int>
#> 1 Adelie    Biscoe     0.5   0.5      NA
#> 2 Adelie    Dream      0.491 0.509     1
#> 3 Adelie    Torgersen  0.511 0.489     5
#> 4 Chinstrap Dream      0.5   0.5      NA
#> 5 Gentoo    Biscoe     0.487 0.513     5

Created on 2021-02-18 by the reprex package (v0.3.0)

@romainfrancois romainfrancois added this to the 1.0.5 milestone Feb 18, 2021
@romainfrancois romainfrancois merged commit a30c9e4 into master Feb 18, 2021
@romainfrancois romainfrancois deleted the across_sequential branch February 18, 2021 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant