Skip to content

Conversation

drizk1
Copy link
Member

@drizk1 drizk1 commented Apr 28, 2025

I noticed in here that pivot_wider cant pivot multiple columns, which I guess is an unstack in dataframes.jl isssue.

Threw together a possible helper function to enable this feature in @pivot_wider

julia> using TidierData, TidierFiles
julia> rent = read_csv("https://raw.githubusercontent.com/tidyverse/tidyr/refs/heads/main/data-raw/us_rent_income.csv")
julia> @chain rent begin 
           @pivot_wider(names_from = variable, 
           values_from = [:estimate, :moe])
           end
52×6 DataFrame
 Row │ GEOID  NAME                  income_estimate  rent_estimate  income_moe  rent_moe 
     │ Int64  String31              String7?         String7?       String3?    String3? 
─────┼───────────────────────────────────────────────────────────────────────────────────
   1 │     1  Alabama               24476            747            136         3
   2 │     2  Alaska                32940            1200           508         13
   3 │     4  Arizona               27517            972            148         4
   4 │     5  Arkansas              23789            709            165         5
   5 │     6  California            29454            1358           109         3
   6 │     8  Colorado              32401            1125           109         5
   7 │     9  Connecticut           35326            1123           195         5

@drizk1
Copy link
Member Author

drizk1 commented Apr 30, 2025

got tidy selection to work with values_from, so this should be good to go now

julia> @chain rent begin 
           @pivot_wider(names_from = variable, 
           values_from = !(GEOID:variable))
           end
52×6 DataFrame
 Row │ GEOID  NAME                  income_estimate  rent_estimate  income_moe  rent_moe 
     │ Int64  String31              String7?         String7?       String3?    String3? 
─────┼───────────────────────────────────────────────────────────────────────────────────
   1 │     1  Alabama               24476            747            136         3
   2 │     2  Alaska                32940            1200           508         13
   3 │     4  Arizona               27517            972            148         4
   4 │     5  Arkansas              23789            709            165         5
   5 │     6  California            29454       

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