Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 26th, 2012  |  syntax: None  |  size: 1.07 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Sum matrix rows with same index
  2. ID, string, float, int  
  3. [...]  
  4. 2038 0;1;2;3;4;5;6;4;2;  898.990    325469692  
  5. 2040 0;1;2;3;4;5;6;4;2;  932.212    346769837  
  6. 2041 0;1;2;3;4;5;6;4;3; 1031.700    400210530  
  7. 2042 0;1;2;3;4;5;6;4;3; 1308.280    510633672  
  8. 2043 0;1;2;3;4;5;6;4;3; 1336.170    480728121
  9.        
  10. XX  0;1;2;3;4;5;6;4;2; 1831.202    672239529  
  11. XY  0;1;2;3;4;5;6;4;3; 3676.15     1391572323
  12.        
  13. dat <- read.table(textConnection("ID, string, float, int  
  14. 2038 0;1;2;3;4;5;6;4;2;  898.990    325469692  
  15. 2040 0;1;2;3;4;5;6;4;2;  932.212    346769837  
  16. 2041 0;1;2;3;4;5;6;4;3; 1031.700    400210530  
  17. 2042 0;1;2;3;4;5;6;4;3; 1308.280    510633672  
  18. 2043 0;1;2;3;4;5;6;4;3; 1336.170    480728121"), header = TRUE)
  19.        
  20. dat <- dat[ , c("string.", "float.", "int")]
  21.        
  22. aggregate( . ~ string., data = dat, sum)
  23.              string.   float.        int
  24. 1 0;1;2;3;4;5;6;4;2; 1831.202  672239529
  25. 2 0;1;2;3;4;5;6;4;3; 3676.150 1391572323
  26.        
  27. library(plyr)
  28. ddply(dat, "string", summarise, floatsum = sum(float), intsum = sum(int))
  29.  
  30. library(reshape)
  31. cast(melt(dat[, -1]), string ~ ..., sum)