I was curious about computation time in R especially while dealing with matrix or data frame. Let's notice the following example:
slowFun = function() {
mat = as.data.frame(matrix(rnorm(10000 * 10), 10000, 10))
mat[, (ncol(mat) + 1)] = NA
for (i in 1:nrow(mat)) {
mat[i,ncol(mat)] = sum(mat[i,], na.rm = T)
}
}
fastFun = function() {
mat = as.data.frame(matrix(rnorm(10000 * 10), 10000, 10))
mat[, (ncol(mat) + 1)] = NA
mat[,ncol(mat)] = rowSums(mat, na.rm = T)
}
system.time(slowFun())
system.time(fastFun())
> system.time(slowFun())
user system elapsed
3.614 0.219 3.837
> system.time(fastFun())
user system elapsed
0.012 0.001 0.013
rowSums was impressive. When I tried to learn all about it:
rowSums
It does not let me see how it was coded! I see .Internal and don't know where to look about it.
Any help will be appreciated.