# Using apply() # Define a matrix m m<-matrix(c(1,2,3,4,5,6,7,8,9,10,11,12), nrow=3, byrow=T) # Display m m # Apply the mean() function to each row apply(m, 1, mean) # Apply the mena() function to each column apply(m, 2, mean) # Create a function to divide by 2 halve<-function(x) x/2 # Apply halve() to each row apply(m, 1, halve) # Returns 4x3, not 3x4 matrix # Apply halve() to each row and transpose t(apply(m,1,halve))