Re: [R] Reshape a matrix

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu, 06 Nov 2008 11:18:28 -0500

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



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. Received on Thu 06 Nov 2008 - 16:30:48 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 06 Nov 2008 - 17:30:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive