#### Functions 2 in R # Find row totals and column totals for a matrix row.totals<-function(X) {t<-seq(1:dim(X)[1]) for (i in 1:dim(X)[1]) {one<- matrix(1,ncol=1,nrow=dim(X)[2]) t[i]<-X[i,]%*%one } # print(t) return(t) } col.totals<- function(X) row.totals(t(X)) # Example A<-matrix(rnorm(8),nrow=4,ncol=2) print(A) row.totals(A) col.totals(A) # Loop for (i in 1:2) { for (j in 2:4) {r<-i;c<-j A<-matrix(rnorm(r*c),nrow=r,ncol=c) # print(i); print(j);print(A) print(row.totals(A)) } }