Data Transform column to Row

looking for a efficient solution for the below scenario.
Column should go as row Value for Locs
Input DF

tribble(
    ~id, ~NY,  ~DC,
    "111",   "a,b,c", "xy,a,b"
)->d

Expecting Output as below

tribble(
    ~id, ~loc,  ~prop,
    "111", "NY",  "a,b,c",
    "111", "DC",  "xy,a,b"
)->d1
cbind(setNames(stack(df1[,-3]),c('sale','v')), group=df1$group)

You can also use tidyr for a more modern solution:

> tidyr::pivot_longer(d, NY:DC, names_to = "loc", values_to = "prop")
# A tibble: 2 x 3
  id    loc   prop  
  <chr> <chr> <chr> 
1 111   NY    a,b,c 
2 111   DC    xy,a,b
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.