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

From: Srinivas Iyyer <srini_iyyer_bio_at_yahoo.com>
Date: Thu, 24 Jan 2008 09:48:18 -0800 (PST)


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. Received on Thu 24 Jan 2008 - 17:56:45 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