The rowSums() function calculates the sum of values in each numeric row of a matrix, array, or data frame.
It quickly sums up data across rows without having to write loops, making it faster and more memory-efficient than manual iteration.
In the above figure, we showed the sum of each row in a data frame using the rowSums() function.
# Create a data frame.
df <- data.frame(
col1 = c(1, 2, 3),
col2 = c(4, 5, 6),
col3 = c(7, 8, 9)
)
# Calculate the row sums.
rowSums(df)
# Output: [1] 12 15 18
The sum of the first row is 12.
The sum of the second row is 15.
The sum of the third row is 18.
rowSums(x, na.rm = FALSE, dims = 1))
Argument | Description |
x | It represents the matrix or data frame. |
na.rm | It is a logical argument. If TRUE, NA values are ignored. By default, NA values are not ignored, and you need to set it to False explicitly. |
dims | It represents the dimensions of x to retain before summation. By default, its value is 1, meaning the function operates over rows. |
If the row contains an NA value, the output will be NA because it does not know what NA means. So, it returns NA.
# Create a data frame.
df <- data.frame(
col1 = c(NA, 2, 3),
col2 = c(4, NA, 6),
col3 = c(7, 8, NA)
)
# Calculate the row sums.
rowSums(df)
# Output: [1] NA NA NA
To ignore the NA values in a data frame and calculate the sum of the remaining values of rows, pass the na.rm = TRUE.
# Create a data frame.
df <- data.frame(
col1 = c(NA, 2, 3),
col2 = c(4, NA, 6),
col3 = c(7, 8, NA)
)
# Calculate the row sums.
rowSums(df, na.rm = TRUE)
# Output: [1] 11 10 9
In the first row, it only adds 4 + 7 and ignores NA. So, the output is 11.
The same thing happened with the second row. It only counts 2 and 8, and their sum is 10.
You can use indexing to select the specific rows for summation. Pass those selections to the rowSums() function. Let’s say I want the sum of row 1 and the sum of row 2.
# Create a data frame.
df <- data.frame(
col1 = c(NA, 2, 3),
col2 = c(4, NA, 6),
col3 = c(7, 8, NA)
)
# Calculate the row sums.
rowSums(df[c(1, 2), ], na.rm = TRUE)
# Output:
# 1 2
# 11 10
In a matrix, there are rows and columns, and rowSums() only focus on rows. If you pass the whole matrix to this function, it will return a vector with the sum of each row.
rv <- rep(1:9)
mtrx <- matrix(rv, 3, 3)
mtrx
cat("The sum of rows is: ", "\n")
rowSums(mtrx)
For other types of row-wise operations (like concatenation of strings), you would use different functions or approaches.
Let’s take a multi-dimensional array as input and find the sum of rows based on dimensions.
arr <- array(1:24, dim = c(2, 3, 4))
rowSums(arr, dims = 1)
# Output: [1] 144 156
rowSums(arr, dims = 2)
# Output:
# [,1] [,2] [,3]
# [1,] 40 48 56
# [2,] 44 52 60
In this code:
What if the input matrix or data frame is empty? Well, in that case, it returns numeric(0) (empty numeric vector).
empty_vec <- matrix(numeric(0), nrow = 0, ncol = 3)
rowSums(empty_vec)
# Output: numeric(0)
The rowSums() function can only operate on numeric columns. If your input data frame contains character columns, it will throw this error: ‘x’ must be numeric.
df <- data.frame(a = c(1, 2), b = c("A", "B")) # Mixed types
rowSums(df)
# Output:
# Error in rowSums(df) : 'x' must be numeric
# Execution halted
Ensure that the input rows are numeric to prevent this type of error, or convert to numeric if compatible.
That’s it!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
The colSums() function in R calculates the sums of columns for numeric matrices, data frames,…
The View() is a utility function in R that invokes a more intuitive spreadsheet-style data…
The summary() is a generic function that produces the summary statistics for various R objects,…
The paste() function in R concatenates vectors after converting them to character. paste("Hello", 19, 21,…
R paste0() function concatenates strings without any separator between them. It is a shorthand version…
Standard Error (SE) measures the variability or dispersion of the sample mean estimate of a…