Table of Contents
The rowSums() function in R is a useful tool for calculating the sum of values in each row of a data frame. This function takes a data frame as input and returns a vector containing the sum of values in each row. It can be used in various scenarios such as analyzing survey data, financial data, and performance metrics. By using the rowSums() function, users can quickly and accurately calculate the total value in each row without having to manually sum up individual values. This function is an efficient and convenient way to perform row-wise calculations in R.
Use rowSums() Function in R
The rowSums() function in R can be used to calculate the sum of the values in each row of a matrix or data frame in R.
This function uses the following basic syntax:
rowSums(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 rowSums() with Data Frame
The following code shows how to use rowSums() to find the sum of the values in each row 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 row
rowSums(df)
[1] 12 9 16 27 24
Example 2: Use rowSums() with NA Values in Data Frame
The following code shows how to use rowSums() to find the sum of the values in each row of a data frame when there are NA values in some rows:
#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 row
rowSums(df, na.rm=TRUE)
[1] 12 7 11 13 24Example 3: Use rowSums() with Specific Rows
The following code shows how to use rowSums() to find the sum of the values in specific rows 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 rows 1, 3, and 5
rowSums(df[c(1, 3, 5), ], na.rm=TRUE)
1 3 5
12 11 24Cite this article
stats writer (2024). How can I use the rowSums() function in R to calculate the sum of values in each row of a data frame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-rowsums-function-in-r-to-calculate-the-sum-of-values-in-each-row-of-a-data-frame/
stats writer. "How can I use the rowSums() function in R to calculate the sum of values in each row of a data frame?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-rowsums-function-in-r-to-calculate-the-sum-of-values-in-each-row-of-a-data-frame/.
stats writer. "How can I use the rowSums() function in R to calculate the sum of values in each row of a data frame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-rowsums-function-in-r-to-calculate-the-sum-of-values-in-each-row-of-a-data-frame/.
stats writer (2024) 'How can I use the rowSums() function in R to calculate the sum of values in each row of a data frame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-rowsums-function-in-r-to-calculate-the-sum-of-values-in-each-row-of-a-data-frame/.
[1] stats writer, "How can I use the rowSums() function in R to calculate the sum of values in each row of a data frame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I use the rowSums() function in R to calculate the sum of values in each row of a data frame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
