1

I have a DF as follows:

'data.frame':   6082 obs. of  3 variables:
 $ ID  : int  23418 23499 24468 24492 24574 24772 25020 25070 25299 25564 ...
 $ Week: Factor w/ 12 levels "77-83 DIM","84-90 DIM","91-97 DIM",....
 $ kg  : num  55.4 56.1 54.9 48 52.8 ...

This is an example:

ID        Week       kg
1 23418 105-111 DIM 55.43655
2 23499 105-111 DIM 56.11586
3 24468 105-111 DIM 54.85228
4 24492 105-111 DIM 47.96415
5 24574 105-111 DIM 52.80463
6 24772 105-111 DIM 49.99884

There is an image attached as well.

I have 610 different ID numbers that are repeated up to 12 time (weeks). But not all the ID numbers have 12 weeks. I need a DF whit 13 columns: ID, 77-83 DIM, 84-90 DIM, 91-97 DIM ... This means one column per each week and a single column for ID. The containing of the table has to be filled with Kg values.

I am sorry if I am not clear enough. Can somebody help me please? Thanks

enter image description here

  • Have a look at: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. Please use `dput(data)` because screenshots are hardly reproducible – Rentrop Apr 15 '16 at 13:20

1 Answers1

3

You can also do this using data.table package.

Assumption: df is the data.frame

Firstly, you have to convert data.frame to data.table by:

> dt <- setDT(df)

After that, you can apply this command to get desired output:

> dcast(dt,ID~Week,value.var='kg')
Kunal Puri
  • 3,419
  • 1
  • 10
  • 22