fake.data <- matrix(rnorm(500), ncol=5) # create fake 100 x 5 data set
head(fake.data,2) # print first two rows
## [,1] [,2] [,3] [,4] [,5]
## [1,] -1.0963542 1.268921 -1.287129 -0.5779126 0.5140325
## [2,] 0.1758044 -1.301273 1.097273 0.7481089 0.2204204
col.sums <- numeric(ncol(fake.data)) # variable to store running column sums
for(i in 1:nrow(fake.data)) {
col.sums <- col.sums + fake.data[i,] # add ith observation to the sum
}
col.sums
## [1] -10.4499437 18.7544425 -0.9722741 -8.9584812 -4.7780701
colSums(fake.data) # A better approach (see also colMeans())
## [1] -10.4499437 18.7544425 -0.9722741 -8.9584812 -4.7780701