How to Use colSums() Function in R


The colSums() function in R can be used to calculate the sum of the values in each column of a matrix or data frame in R.

This function uses the following basic syntax:

colSums(x, na.rm=FALSE)

where:

  • x: Name of the matrix or data frame.
  • na.rm: Whether to ignore NA values. Default is FALSE.

The following examples show how to use this function in practice.

Example 1: Use colSums() with Data Frame

The following code shows how to use colSums() to find the sum of the values in each column of a data frame:

#create data frame
df <- data.frame(var1=c(1, 3, 3, 4, 5),
                 var2=c(7, 2, 5, 3, 2),
                 var3=c(3, 3, 6, 6, 8),
                 var4=c(1, 1, 2, 14, 9))

#view data frame
df

  var1 var2 var3 var4
1    1    7    3    1
2    3    2    3    1
3    3    5    6    2
4    4    3    6   14
5    5    2    8    9

#find sum of each column
colSums(df)

var1 var2 var3 var4 
  16   19   26   27 

Here’s how to interpret the output:

  • The sum of values in the ‘var1’ column is 16.
  • The sum of values in the ‘var2’ column is 19.
  • The sum of values in the ‘var3’ column is 26.
  • The sum of values in the ‘var4’ column is 27.

Example 2: Use colSums() with NA Values in Data Frame

The following code shows how to use colSums() to find the sum of the values in each column of a data frame when there are NA values in some columns:

#create data frame with some NA values
df <- data.frame(var1=c(1, 3, 3, 4, 5),
                 var2=c(7, NA, NA, 3, 2),
                 var3=c(3, 3, 6, 6, 8),
                 var4=c(1, 1, 2, NA, 9))

#view data frame
df

  var1 var2 var3 var4
1    1    7    3    1
2    3   NA    3    1
3    3   NA    6    2
4    4    3    6   NA
5    5    2    8    9

#find sum of each column
colSums(df, na.rm=TRUE)

var1 var2 var3 var4 
  16   12   26   13 

Example 3: Use colSums() with Specific Columns

The following code shows how to use colSums() to find the sum of the values in specific columns of a data frame:

#create data frame with some NA values
df <- data.frame(var1=c(1, 3, 3, 4, 5),
                 var2=c(7, NA, NA, 3, 2),
                 var3=c(3, 3, 6, 6, 8),
                 var4=c(1, 1, 2, NA, 9))

#view data frame
df

  var1 var2 var3 var4
1    1    7    3    1
2    3   NA    3    1
3    3   NA    6    2
4    4    3    6   NA
5    5    2    8    9

#find sum of columns 1, 3, and 4
colSums(df[, c(1, 3, 4)], na.rm=TRUE)

var1 var3 var4 
  16   26   13

Additional Resources

How to Sum Specific Columns in R (With Examples)
How to Sum Specific Rows in R (With Examples)

Leave a Reply

Your email address will not be published.