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.

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

Reply via email to