Data Transform column to Row

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

    ~id, ~NY,  ~DC,
    "111",   "a,b,c", "xy,a,b"

Expecting Output as below

    ~id, ~loc,  ~prop,
    "111", "NY",  "a,b,c",
    "111", "DC",  "xy,a,b"
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

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.