colSums, colMeans, rowSums & rowMeans in R (Example)
This page shows how to apply the colSums, colMeans, rowSums, and rowMeans functions to compute the sums and means of all columns and rows of a data frame in the R programming language.
Example Data
my_data <- data.frame(x = c(3, 7, 1, 5), y = c(9, 5, 2, 8), z = c(1, 9, 1, 4)) my_data # x y z # 3 9 1 # 7 5 9 # 1 2 1 # 5 8 4 |
my_data <- data.frame(x = c(3, 7, 1, 5), y = c(9, 5, 2, 8), z = c(1, 9, 1, 4)) my_data # x y z # 3 9 1 # 7 5 9 # 1 2 1 # 5 8 4
Sums of Columns
colSums(my_data) # x y z # 16 24 15 |
colSums(my_data) # x y z # 16 24 15
Means of Columns
colMeans(my_data) # x y z # 4.00 6.00 3.75 |
colMeans(my_data) # x y z # 4.00 6.00 3.75
Sums of Rows
rowSums(my_data) # 13 21 4 17 |
rowSums(my_data) # 13 21 4 17
Means of Rows
rowMeans(my_data) # 4.333333 7.000000 1.333333 5.666667 |
rowMeans(my_data) # 4.333333 7.000000 1.333333 5.666667
Video Examples
This video shows further examples for the application of colSums, colMeans, rowSums, and rowMeans.