Advertisement
Guest User

stupid.script.R

a guest
Apr 4th, 2013
1,445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.98 KB | None | 0 0
  1. # create big data frame:
  2. n <- 1000
  3. x <- data.frame(group = sample(letters[1:4], n, replace=TRUE), condition = sample(LETTERS[1:10], n, replace = TRUE), data = rnorm(n))
  4.  
  5. # reasonable operations:
  6. marginal.means.1 <- aggregate(data ~ group + condition, data = x, FUN=mean)
  7.  
  8. # unreasonable operations:
  9. marginal.means.2 <- marginal.means.1[NULL,]
  10.  
  11. row.counter <- 1
  12. for (condition in levels(x$condition)) {
  13.   for (group in levels(x$group)) {  
  14.     tmp.value <- 0
  15.     tmp.length <- 0
  16.     for (c in 1:nrow(x)) {
  17.       if ((x[c,"group"] == group) & (x[c,"condition"] == condition)) {
  18.         tmp.value <- tmp.value + x[c,"data"]
  19.         tmp.length <- tmp.length + 1
  20.       }
  21.     }
  22.     marginal.means.2[row.counter,"group"] <- group
  23.     marginal.means.2[row.counter,"condition"] <- condition
  24.     marginal.means.2[row.counter,"data"] <- tmp.value / tmp.length
  25.     row.counter <- row.counter + 1
  26.   }
  27. }
  28.  
  29. # does it produce the same results?
  30. all.equal(marginal.means.1, marginal.means.2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement