[R] Filling data frame data into a matrix - please help
Srinivas Iyyer
srini_iyyer_bio at yahoo.com
Thu Jan 24 19:35:29 CET 2008
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
--- Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> 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?
More information about the R-help
mailing list