[R] Filling in a Zero Matrix

jim holtman jholtman at gmail.com
Mon Nov 26 22:07:05 CET 2007


You can use the reshape package:

> x <- read.table(textConnection("Samp             MCI               PCT
+ 1                    1                     2
+ 1                   4                     5
+ 1                   7                     8
+ 2                   4                     4
+ 2                   7                     5
+ 2                    8                     7"), header=TRUE)
> require(reshape)
> x.m <- melt(x, measure.var="PCT")
> cast(x.m, MCI ~ Samp)
  MCI  1  2
1   1  2 NA
2   4  5  4
3   7  8  5
4   8 NA  7
>


On Nov 26, 2007 11:50 AM, Amit Patel <amitpatel_ak at yahoo.co.uk> wrote:
>
> Hi
>
> The data is in the format
>
> Samp             MCI               PCT
> 1                    1                     2
> 1                   4                     5
> 1                   7                     8
> 2                   4                     4
> 2                   7                     5
> 2                    8                     7
>
> I have split the columns into individual vectors
> I would like the data in the format
>
> MCI          Samp1         Samp2
> 1                  2                  NA
> 4                  5                  4
>  7                  8                  5
> 8                  NA               7
>
>  Kind Regards
> Amit Patel
>
>
> ----- Original Message ----
> From: jim holtman <jholtman at gmail.com>
> To: Amit Patel <amitpatel_ak at yahoo.co.uk>
> Sent: Monday, 26 November, 2007 3:12:19 PM
> Subject: Re: [R] Filling in a Zero Matrix
>
> Could you please provide commented, minimal, self-contained,
> reproducible code and an example of what you would like as output.  We
> don't have the test file that you are trying to read, nor really
> understand what you would like as output.
>
> On Nov 26, 2007 9:31 AM, Amit Patel <amitpatel_ak at yahoo.co.uk> wrote:
> > Hi
> > I am very new to R and statistical programming in general. I am trying to
> reorder data from a .csv file. I have managed to import the data and create
> a zero matrix. I am now trying to fill the matrix. There seems to be some
> problem with this section of my code. I have highlighted the dodgy code in
> red. Please help if possible.
> >
> > ######################################################
> > ###########  Create matrix ###########################
> > ######################################################
> >
> > #Open the csv file
> > OGSdata <- read.table("MG3199.csv",sep=",",header=TRUE)
> >
> > #creates 3 separate vectors
> > sample <- OGSdata[,1]
> > mci <- OGSdata[,2]
> > pct <- OGSdata[,3]
> >
> > #change mci range
> > offset <- min(mci)-1
> > mci <- (mci - offset)
> >
> > #matrix sizes
> > mci_count <- max(mci)
> > sample_count <- max(sample)
> >
> > #creates a zero matrix
> > OGS <- mat.or.vec(mci_count,sample_count)
> >
> > #Create labels
> > sample_lab <- ("A-9","B-9", "C-9", "D-9", "E-9","A-12","B-12", "C-12",
> "D-12", "E-12")
> >
> > #add data
> > for (i in 1:length(pct)) {
> >    OGS(mci(i),sample(i))<- pct(i);
> > }
> >
> >
> > What I want is to have colum1 from original data to be the xlabel, column
> 2 to be the ylabel and the 3rd colum to be the values in the matrix
> > Any help is appreciated.
> >
> > Kind Regards
> > Amit Patel
> >
> >
> >
> >
> >      ___________________________________________________________
> >
> > now.
> >
> >        [[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.
> >
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>
>
> ________________________________
> Yahoo! Mail now has unlimited storage, which means you can have spam control
> and more space for those important e-mails.



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list