Skip to content

reshape a matrix

9 messages · wendy, David Winsemius, Rolf Turner +2 more

#
Hi all,

I have a data.frame like following

A<-c('d0','d0','d1','d1','d2','d2')
B<-rep(c('control','sample'),3)
C<-c(rep(100000,2),200,300,400,500)
dataframe<-data.frame(A,B,C)

I want to reshape the matrix, so the matrix with 'd0', 'd1' and 'd2' in rows
and 'control' and 'sample' in columns. Is there a function for doing this
easily?

Thank you in advance.

Wendy

--
View this message in context: http://r.789695.n4.nabble.com/reshape-a-matrix-tp3757179p3757179.html
Sent from the R help mailing list archive at Nabble.com.
#
On 20.08.2011 17:04, Wendy wrote:
See ?reshape

reshape(data=dataframe, idvar="A", timevar="B", direction="wide")

Uwe Ligges
#
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.)
#
On 20.08.2011 19:04, David Winsemius wrote:
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?

Best,
Uwe
>
 > Your data is all ready "molten", i.e. it is in the long format
#
On Aug 20, 2011, at 1:21 PM, Uwe Ligges wrote:

            
The stumbling blocks I have encountered are in trying to figure out  
which of the multiple arguments are needed a) in going from wide to  
long and b) in going from long to wide, c) and what are the reasons  
for the various error messages I provoke . I am almost never able to  
do it correctly on the first try and rarely able to do it even on the  
fourth try. I bought Spector's book in hopes of understanding it  
better, but his efforts did not take root in my brain.

In the instance above, how would I have know how to apply the help  
page description of timevar (below) to this problem?

timevar
the variable in long format that differentiates multiple records from  
the same group or individual.

To my reading that does not distinguish the purpose of 'timevar' from  
the purpose of 'idvar' and then reading the 'idvar' definition just  
below it does not help at all.
#
On Aug 20, 2011, at 1:39 PM, David Winsemius wrote:

            
I'm wondering it this would be clearer (if it is correct):

timevar
the variable in long format that order multiple records which vary  
within the same groups or individual specified in "idvar".
#
On 21/08/11 03:04, Wendy wrote:
Your question has been answered .... But if you remember that a *matrix*
and a *data frame* are ***NOT*** the same thing, you'll get into a lot less
trouble.  Why do you think there are two *different* terms for these sorts
of object if they are the same thing?  Psigh!!!

     cheers,

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

            
[snip original problem]
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.
Michael Dewey
info at aghmed.fsnet.co.uk
http://www.aghmed.fsnet.co.uk/home.html
#
On 21.08.2011 12:54, Michael Dewey wrote:
In think tha major problem is that stuff like "id" and "time" etc. may 
be misleading, if they do not fit to the context. Don't believe I do 
things correct right away here. Nevertheless, improving the wording of 
the reshape() documentation is not easy at all if we want to be precise. 
Maybe we find another volunteer or I find some time to write a short 
column for the next issue of The R Journal.

Best wishes,
Uwe