[R] reshape a matrix

David Winsemius dwinsemius at comcast.net
Sun Aug 21 01:06:33 CEST 2011


On Aug 20, 2011, at 1:39 PM, David Winsemius wrote:

>
> On Aug 20, 2011, at 1:21 PM, Uwe Ligges wrote:
>
>>
>>
>> On 20.08.2011 19:04, David Winsemius wrote:
>>>
>>> On Aug 20, 2011, at 12:32 PM, Uwe Ligges wrote:
>>>
>>>> On 20.08.2011 17:04, Wendy wrote:
>>>>> 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?
>>>>
>>>> See ?reshape
>>>>
>>>> reshape(data=dataframe, idvar="A", timevar="B", direction="wide")
>>>>
>>>> Uwe Ligges
>>>
>>> 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.
>>
>> David,
>>
>> I think there are some good examples on the help page. What is  
>> missing? What is not clearly explained?
>
> 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.

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".

-- 
David


>
> 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.
>
> -- 
> David.
>
>
>> If a longer tutorial is needed, that may be an article for the R  
>> Help Desk in The R Journal. Anybody volunteering?
>>
>> 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.)
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list