Re: [R] Filling data frame data into a matrix - please help

From: Srinivas Iyyer <srini_iyyer_bio_at_yahoo.com>
Date: Thu, 24 Jan 2008 10:35:29 -0800 (PST)


Dear Henrique,
thanks for the tip.

One question though: how do I get back a matrix out of d1.tb.
d1.tb is an xtabs object.

I tried:

myres <- ftable(d1.tb)
myre1 <- as.data.frame(myres)

now myres1 is nor a matrix or a df.

I want d1.tb as a matrix.

Please suggest.

thanks
srini

> Try this:
>
> d1 <- data.frame(d1)
> d1$V3 <- as.numeric(as.character(d1$V3))
> xtabs(V3 ~ r1+c1, data=d1)
>
> You can substitute 0 with NA:
>
> d1.tb <- xtabs(V3 ~ r1+c1, data=d1)
> d1.tb[d1.tb==0] <- NA
>
>
> On 24/01/2008, Srinivas Iyyer
> <srini_iyyer_bio_at_yahoo.com> wrote:
> > Dear group,
> > I have a data.frame (d1) with various elements and
> a
> > matrix (m1) created with NA (or 1s). I want to
> read
> > each row,column in d1 and fill its numeric value
> in
> > m1.
> >
> > Could some one help me because I have 130K rows
> and
> > 500 column data.frame object and I was told that a
> for
> > loop will take a long time.
> >
> > Thank you.
> >
> > > r1 <- c("A","A","B","B")
> > > c1 <- c("Apple","Andy","Boy","Ball")
> > > v1 <- as.numeric(c(0.001,0.02,0.3,NA))
> > > d1 <- cbind(r1,c1,as.numeric(v1))
> > > d1 <- data.frame(d1)
> > > d1
> > r1 c1 V3
> > 1 A Apple 0.001
> > 2 A Andy 0.02
> > 3 B Boy 0.3
> > 4 B Ball <NA>
> >
> >
> > > m1 <- matrix(NA,2,4)
> > > rownames(m1)<- unique(r1)
> > > colnames(m1)<-unique(c1)
> > > m1
> > Apple Andy Boy Ball
> > A NA NA NA NA
> > B NA NA NA NA
> >
> >
> > I want to have:
> >
> > result:
> >
> >
> > Apple Andy Boy Ball
> > A 0.001 0.02 NA NA
> > B NA NA 0.3 NA
> >
> >
> > thanks
> > Srini
> >
> >
> >
>



> > Be a better friend, newshound, and
> >
> > ______________________________________________
> > 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.
> >
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>


Looking for last minute shopping deals?

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 24 Jan 2008 - 18:40:00 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 24 Jan 2008 - 19:30:08 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