0

I want to use reshape2 to cast or dcast my data.

This is the input:

  ID_1 Naam_1   Naam_2 Naam_3   Naam_4 Oud Oud_1 Oud_2 Oud_3 Oud_4
1    1  Peter Primrose  Julia Princess  22    37    30    36    16
2    2 Sussie    Johan   Veli      Jan  25    89    15    14    23
3    3   Jane   tarsan  Smith     <NA>  35    21    NA    NA    NA
4    4    San     <NA>   <NA>     <NA>  30    NA    NA    NA    NA
5    5    hey  Morning    jsj      uij  45    45    45    65    78

The output should like this...

  ID_1 variable     Naam Oud
1     1        1    Peter  37
2     2        1   Sussie  89
3     3        1     Jane  21
5     5        1      hey  45
6     1        2 Primrose  30
7     2        2    Johan  15
10    5        2  Morning  45
11    1        3    Julia  36
12    2        3     Veli  14
15    5        3      jsj  65
16    1        4 Princess  16
17    2        4      Jan  23
20    5        4      uij  78
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 1
    What happened to `Oud` column? – zx8754 Aug 01 '16 at 09:19
  • Relevant post: [Reshape data from wide to long format](http://stackoverflow.com/questions/1181060) – zx8754 Aug 01 '16 at 09:24
  • 1
    You can use `data.table`'s melt function which accepts multiple patterns. It would then be `library(data.table); melt(setDT(df), measure.vars = patterns("^Naam", "^Oud"), value.name = c("Naam", "Oud"))`. There must be a duplicate question for this – talat Aug 01 '16 at 09:27

0 Answers0