[R] Reshape a matrix

Gabor Grothendieck ggrothendieck at gmail.com
Thu Nov 6 17:18:28 CET 2008


Assuming you mean data frame and not matrix and
depending on whether the empty spaces are intended
to represent NA or 0 we have:

> DF <- data.frame(V1 = LETTERS[1:3], V2 = LETTERS[24:26], V3 = 1:3)
> tapply(DF[[3]], DF[1:2], c)
   V2
V1   X  Y  Z
  A  1 NA NA
  B NA  2 NA
  C NA NA  3
> xtabs(V3 ~ V1 + V2, DF)
   V2
V1  X Y Z
  A 1 0 0
  B 0 2 0
  C 0 0 3


On Wed, Nov 5, 2008 at 7:53 PM, dinesh kumar <barupal at gmail.com> wrote:
> Dear R users,
>
> I have a matrix like
>
> A      X    1
> B      Y    2
> C      Z    3
>
> I want to reshape this matrix into this format
>
>    X      Y      Z
> A  1
> B         2
> C                 3
>
>
>
> Thanks in advance for your help.
>
>
> Dinesh
>
> --
> Dinesh Kumar Barupal
> Junior Specialist
> Metabolomics Fiehn Lab
> UCD Genome Center
> 451 East Health Science Drive
> GBSF Builidng
> University of California
> DAVIS
> 95616
> http://fiehnlab.ucdavis.edu/staff/kumar
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list