How to Add Row and Column Totals in R - dummies

How to Add Row and Column Totals in R

By Andrie de Vries, Joris Meys

One task that you may frequently do in a spreadsheet that you can also do in R is calculating row or column totals. The easiest way to do this is to use the functions rowSums() and colSums().Similarly, use the functions rowMeans() and colMeans() to calculate means.

Try it on the built-in dataset iris. First, remove the fifth column, because it contains text that describes the species of iris:

> iris.num <- iris[, -5]

Then calculate the sum and mean for each column:

> colSums(iris.num)
> colMeans(iris.num)

These two functions are very convenient, but you may want to calculate some other statistic for each column or row. There’s an easy way of traversing rows or columns of an array or data frame: the apply() For example, getting the minimum of a column is the same as applying a min() function to the second dimension of your data:

> apply(iris.num, 2, min)
> apply(iris.num, 2, max)

The apply() function is ideal when your data is in an array and will apply happily over both rows and columns. For the special case where your data is in a data frame and you want to get column summaries, you’re better off using sapply() rather than apply(). So, to get your iris column summaries, try this instead:

> sapply(iris.num, min)
> sapply(iris.num, max)