0

Hi I have been trying to transpose the data which have the structure like below;

ID Seq PRODUCT  
A1  1     A
A1  2     B
A1  3     C
A1  4     D
A1  5     E
A2  1     C
A2  2     D
A2  3     E
A2  4     F
A3  1     A
A3  2     C
A3  3     D

and I need the new structure like this

ID   1   2   3   4   5
A1   A   B   C   D   E
A2   C   D   E   F
A3   A   C   D 

I have tried this code, but the output is still incorrect.

dt <- reshape(mydata,  v.names = "Seq" ,timevar = "Seq", direction = "wide", idvar = "ID")

Recently I have tested

reshape(mydata,  ,timevar = "Seq", direction = "wide", idvar = "ID")

but it seemed like my actual data have long "Seq", so the data is grouped in to a single column like below. How to expand the column? My actual data have maximum sequence around 30 sequences.

ID PROD 1:5 
A1 NA
A2 NA
A3 NA
  • 1
    `library(tidyr); df %>% group_by(ID) %>% spread(Seq, PRODUCT)` – Adam Quek Oct 19 '17 at 03:50
  • You just want `reshape(mydata, idvar="ID", timevar="Seq", direction="wide")` – thelatemail Oct 19 '17 at 03:51
  • Your only real mistake was you should have specified `v.names = "PRODUCT"` as the variable you wanted to convert from long to wide. By specifying `"Seq"` instead, you ending up converting the same variable used to define the times into long format, and left `PRODUCT` to be considered a constant value in each group. – thelatemail Oct 19 '17 at 04:02
  • Thanks a lot I tried reshape(mydata, idvar="ID", timevar="Seq", direction="wide") , but it seems like my Seq are pity long so they a grouped in a single column. How can I expand my column – Siroros Roongdonsai Oct 19 '17 at 04:26
  • Thanks so much I have already solved my problem . Data Table seems to be more efficient. https://stackoverflow.com/a/36253517/8057588 – Siroros Roongdonsai Oct 19 '17 at 04:45

0 Answers0