# How to Use the margin.table() Function in R

Often in data analysis, you will be given a matrix that contains rows and columns and you’ll be interested in calculating the row sums or column sums.

One common way to do so is by using the margin.table() function available in base R.

This function uses the following basic syntax:

margin.table(x, margin)

where:

• x: The name of the array or matrix
• margin: The margin sum to calculate (1 = row sum, 2 = column sum)

Note that if you leave out the margin argument, then the margin.table() function will simply calculate the sum of all values in the table.

Note: The margin.table() function is available in base R, which means you don’t need to load any external libraries to use it.

This tutorial provides several examples of how to use this function in practice.

## Example 1: Use margin.table() to Calculate Row Sums in Matrix

Suppose we have the following matrix that contains 4 rows and 5 columns:

```#create matrix with 4 rows
my_matrix <- matrix(1:20, 4)

#view matrix
my_matrix

[,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20
```

Suppose that we would like to calculate the sum of each row in the matrix.

We can use the margin.table() function with the argument margin=1 to do so:

```#calculate row sums of matrix
margin.table(my_matrix, 1)

[1] 45 50 55 60```

The output displays the sum of each row in the matrix.

For example:

• Sum of row 1: 1 + 5 + 9 + 13 + 17 = 45
• Sum of row 2: 2 + 6 + 10 + 14 + 18 = 50
• Sum of row 3: 3 + 7 + 11 + 15 + 19 = 55
• Sum of row 4: 4 + 8 + 12 + 16 + 20 = 60

And so on.

If you’d like, you could use the cbind function to add these row sums as a new column at the end of the matrix:

```#add row sums to matrix
my_matrix <- cbind(my_matrix, margin.table(my_matrix, 1))

#view updated matrix
my_matrix

[,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    5    9   13   17   45
[2,]    2    6   10   14   18   50
[3,]    3    7   11   15   19   55
[4,]    4    8   12   16   20   60
```

Notice that the last column now contains the row sums calculated by the margin.table() function.

## Example 2: Use margin.table() to Calculate Column Sums in Matrix

Once again, suppose we have the following matrix that contains 4 rows and 5 columns:

```#create matrix with 4 rows
my_matrix <- matrix(1:20, 4)

#view matrix
my_matrix

[,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20
```

Suppose that we would like to calculate the sum of each column in the matrix.

We can use the margin.table() function with the argument margin=2 to do so:

```#calculate column sums of matrix
margin.table(my_matrix, 2)

[1] 10 26 42 58 74```

The output displays the sum of each column in the matrix.

For example:

• Sum of column 1: 1 + 2 + 3 + 4 = 10
• Sum of column 2: 5 + 6 + 7 + 8 = 26
• Sum of column 3: 9 + 10 + 11 + 12 = 42
• Sum of column 4: 13 + 14 + 15 + 16 = 58
• Sum of column 5: 17 + 18 + 19 + 20 = 74

And so on.

## Example 3: Use margin.table() to Calculate Column Sum of All Values in Matrix

Once again, suppose we have the following matrix that contains 4 rows and 5 columns:

```#create matrix with 4 rows
my_matrix <- matrix(1:20, 4)

#view matrix
my_matrix

[,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20
```

Suppose that we would like to calculate the sum of each value in the matrix.

We can use the margin.table() function with no value specified for the margin argument:

```#calculate sum of all values in matrix
margin.table(my_matrix)

[1] 210
```

From the output we can see that the sum of all values in the matrix is 210.