0

Hi all I have a table

id  sample_name replicates  raw 
a   control     c1          1   
a   control     c2          2   
a   control     c3          3   
b   control     c1          10    
b   control     c2          20   
b   control     c3          20   

and would like to transform this to

id c1   c2   c3 
a  1    2    3    
b  10   20   20 

The only way I know how to do this is to brute loop through every 3, because there are 3 replicates and append that to multiple vectors and recombine it at the end into a dataframe, thus I'm wondering if there is a more elegant/simple way to do this? thanks!

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Ahdee
  • 4,679
  • 4
  • 34
  • 58

1 Answers1

1

Try this...

> library(reshape2)
> dcast(melt(df[, -2]), ...~replicates)[, -2]
Using id, replicates as id variables
  id c1 c2 c3
1  a  1  2  3
2  b 10 20 20
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138