#create this matrix
mymat<-matrix(1:20,nrow=5,byrow=T)
#
# it should look like this
# [,1] [,2] [,3] [,4]
#[1,] 1 2 3 4
#[2,] 5 6 7 8
#[3,] 9 10 11 12
#[4,] 13 14 15 16
#[5,] 17 18 19 20
# compute the column sums 3 different ways:
#a) with 2 nested loops but without any functions like sum() or apply()
#b) using one loop and you are allowed to use a function such as sum()
#c) you are not allowed to use any loops but you will likely need to use apply()
for(i in 1:4){
mysum<-0
for(j in 1:5){
mysum<-mysum+mymat[j,i]
}
cat("sum1: ",mysum,"\n")
}
for(i in 1:4){
mysum<-sum(mymat[,i])
cat("sum2: ",mysum,"\n")
}
mysum<-apply(mymat,2,sum)
cat("sum3: ",mysum,"\n")
back