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

Srinivas Iyyer srini_iyyer_bio at yahoo.com
Thu Jan 24 18:48:18 CET 2008

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

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:


  Apple   Andy  Boy  Ball
A 0.001   0.02  NA    NA
B  NA     NA    0.3   NA


Be a better friend, newshound, and

More information about the R-help mailing list