[R] copy matrix another matrix

MSousa ricardosousa2000 at clix.pt
Thu Mar 29 18:01:33 CEST 2012




> results
   val_user v_source v_destine count average
1     7       22        22     4  186.25
2     7       22        22     2  383.50
3     7       22        22     1   19.00
4     7       97        97     1  196.00
5     7      161        97     1   68.00
6     7       22       130     1  219.00
7     7      130       130     1  219.00
8     8       22        22     1   28.00
9     8       22        22     1    9.00
10    8       97        22     1  427.00
…...

 #create matrix hits
  matrix_hits<-matrix(0,nrow=nrow(resumo),ncol=nrow(resumo),)
  colnames(matrix_hits)<-resumo$Apcode
  rownames(matrix_hits)<-resumo$Apcode
 

users<-levels
users<-data.frame(levels(factor(results$val_user)) 
for(val_user in users)
{
   results_user <- results[results$val_user==val_user,]
  r1<-tapply(results_user$count,
list(results_user$v_source,results_user$v_destine),sum )
  r1[is.na(r1)] <- 0 

  for(i in 1:nrow(r1))
  {
    for(j in 1:ncol(r1))
    {
      print(paste(rownames(r1)[i],colnames(r1)[j]))
      col<-colnames(r1)[j]
      row<-rownames(r1)[i]
      hits<-r1[row,col]
      matrix_hits[col,row]<- as.numeric(matrix_hits[col,row])+hits
    }
  }
}

View(matrix_hits)

is there any way to do this more effectively,  maintaining the loop for the
user, updating only  matrix hits  with results the matrix r1

Thanks

--
View this message in context: http://r.789695.n4.nabble.com/copy-matrix-another-matrix-tp4515909p4515909.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list