Skip to content
Prev 269180 / 398502 Next

reshape a matrix

On Aug 20, 2011, at 12:32 PM, Uwe Ligges wrote:

            
Many people have experienced problems understanding the mechanics of  
the base function 'reshape'. If you do not and if do continue to use  
it, you would be doing the world a great service by writing a tutorial  
manual with a bunch of worked examples. I have never found a tutorial  
that clarified how I should use it in the variety of situations where  
I have needed it.

  So Hadley wrote an alternate facility   ... the reshape package that  
does not have a reshape function in it but rather two functions 'melt'  
and 'cast'. Your data is all ready "molten", i.e. it is in the long  
format (in the terminology of the base reshape function) with  
identifier values in each row and a single column of values.

 > library(reshape)
 > cast(dataframe,A~B)
Using C as value column.  Use the value argument to cast to override  
this choice
    A control sample
1 d0   1e+05  1e+05
2 d1   2e+02  3e+02
3 d2   4e+02  5e+02

Basically the cast formula keeps the LHS variables in the rows and hte  
RHD variables get arranges in columns. (For reasons that are unclear  
to me the dataframe argument was placed first when using positional  
argument passing, unlike most other formula methods in R.)