1
df45 = data.frame(
       Tag = c(1,1,1,1,1,2,2,2,2,2),
       clus = c("L1","L2","L3","L4","L5","L1","L2","L3","L4","L5"),
       long = runif(1:10,1,2),
       medium = runif(1:10,1,2),
       short = runif(1:10,1,2),
       urgent = runif(1:10,1,2))

here is the data i am trying to pivot .

enter image description here

This is the expected Output is shown image ,To start of with i tried with below code but i am unable achieve the required result.

    melt(df45 , id.var = "Tag")
Nabi Shaikh
  • 787
  • 1
  • 6
  • 26
  • 3
    Try `library(tidyverse); df45 %>% gather(label, val, -c(Tag, clus)) %>% spread(clus, val)` – Sotos Feb 16 '18 at 11:02
  • @Sotos I wonder if this could be achieved just using the `reshape` function in base R. – tushaR Feb 16 '18 at 11:44
  • @TUSHAr Ofcourse. It is a combination of dcast and melt. I think they made one function that does both called `recast` so it can probably be done in one line too. It's been a while since I have used reshaoe though – Sotos Feb 16 '18 at 11:47
  • Sorry I thought you meant reshape the package. I have never used the base reshape – Sotos Feb 16 '18 at 11:48
  • @TUSHAr yes i was also trying the same with reshape .. and rightly its a combination of dcast and melt – Nabi Shaikh Feb 16 '18 at 12:08
  • @NabiShaikh So far I have not been able to solve achieve the results using `reshape`. It's very complicated. – tushaR Feb 16 '18 at 12:09
  • @Sotos, The concept is the same in this case: make the data long and then go wide. `reshape(cbind(df45[c(1, 2)], stack(df45[-c(1, 2)])), direction = "wide", idvar = c("Tag", "ind"), timevar = "clus")`. But there are cleaner ways to do it with the packages we have available today. – A5C1D2H2I1M1N2O1R2T1 Feb 20 '18 at 16:35

0 Answers0