Skip to content

Conversation

@DavisVaughan
Copy link
Member

Closes #839

Implements names_vary, as described compactly by this comment #839 (comment)

The actual coding of this is fairly straightforward. It just swaps vec_rep() for vec_rep_each() and vice versa while creating the spec columns.

As shown in #839 (comment), this can currently be accomplished using arrange() on a spec generated by build_wider_spec(). But this feels like it might be worth adding an argument for, since it seems to come up fairly often (2 issues and at least 2 stack overflow questions).

@DavisVaughan
Copy link
Member Author

One of the reasons I think this is useful is that, when combined with names_glue, it allows us to generate all 4 of the "common" combinations of names that a user might want

library(tidyr)

# - {values_from}_{names_from}
# - names_from varies fastest
us_rent_income %>% 
  pivot_wider(
    names_from = variable, 
    values_from = c(estimate, moe)
  ) %>%
  names()
#> [1] "GEOID"           "NAME"            "estimate_income" "estimate_rent"  
#> [5] "moe_income"      "moe_rent"

# - {names_from}_{values_from}
# - names_from varies fastest
us_rent_income %>% 
  pivot_wider(
    names_from = variable, 
    values_from = c(estimate, moe),
    names_glue = "{variable}_{.value}"
  ) %>%
  names()
#> [1] "GEOID"           "NAME"            "income_estimate" "rent_estimate"  
#> [5] "income_moe"      "rent_moe"

# - {values_from}_{names_from}
# - names_from varies slowest
us_rent_income %>% 
  pivot_wider(
    names_from = variable, 
    values_from = c(estimate, moe),
    names_vary = "slowest"
  ) %>%
  names()
#> [1] "GEOID"           "NAME"            "estimate_income" "moe_income"     
#> [5] "estimate_rent"   "moe_rent"

# - {names_from}_{values_from}
# - names_from varies slowest
us_rent_income %>% 
  pivot_wider(
    names_from = variable, 
    values_from = c(estimate, moe),
    names_vary = "slowest",
    names_glue = "{variable}_{.value}"
  ) %>%
  names()
#> [1] "GEOID"           "NAME"            "income_estimate" "income_moe"     
#> [5] "rent_estimate"   "rent_moe"

Created on 2021-12-14 by the reprex package (v2.0.1)

@DavisVaughan DavisVaughan requested a review from hadley December 14, 2021 21:15
@DavisVaughan
Copy link
Member Author

@hadley I'll leave this up to you as to whether you think this is the best way forward for that issue

Copy link
Member

@hadley hadley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it!

@DavisVaughan DavisVaughan merged commit 59ce0b9 into tidyverse:main Dec 15, 2021
@DavisVaughan DavisVaughan deleted the feature/values-vary branch December 15, 2021 18:31
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.

FR: order of columns resulting from pivot_wider

2 participants