[R] reshape a matrix

Michael Dewey info at aghmed.fsnet.co.uk
Sun Aug 21 12:54:29 CEST 2011


At 18:21 20/08/2011, Uwe Ligges wrote:


>On 20.08.2011 19:04, David Winsemius wrote:
>>
>>On Aug 20, 2011, at 12:32 PM, Uwe Ligges wrote:

[snip original problem]

>David,
>
>I think there are some good examples on the help page. What is 
>missing? What is not clearly explained? If a longer tutorial is 
>needed, that may be an article for the R Help Desk in The R Journal. 
>Anybody volunteering?

Uwe
I think the problem is that those of us who do not understand are in 
such a state of ignorance that we do not know what it is that we do 
not understand. I have resigned myself to the realisation that there 
are a very small number of things about R which I shall never 
understand and always solve by trial and error and the 
parameterisation of reshape is the leading example (closely followed 
by backslashes). I do not think the writers of the documentation are 
at fault here, it is either just inherently difficult to understand 
or my cortex is wired up inappropriately.

>Best,
>Uwe
>
>
>>
>>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.)
>
>

Michael Dewey
info at aghmed.fsnet.co.uk
http://www.aghmed.fsnet.co.uk/home.html



More information about the R-help mailing list