rowSums Function in detail
The rowSums() function in R is used to compute the sum of rows in a matrix, array, or data frame. It is particularly useful when dealing with large datasets where row-wise summation is required.
Syntax:
rowSums(x, na.rm = FALSE, dims = 1)
Parameters:
- x: A matrix, array, or data frame.
- na.rm: A logical argument. If set to
TRUE, it removes missing values (NA) before calculating the sum. Default isFALSE. - dims: An integer specifying the dimensions regarded as ‘rows’ to sum over. It applies summation over
dims+1, dims+2, ...
Compute the Sum of Rows of a Matrix in R
Let’s create a matrix and use rowSums() to calculate the sum of its rows.
Example:
# Creating a matrix
mat <- matrix(c(3, 6, 9, 4, 7, 10, 5, 8, 11), nrow = 3, byrow = TRUE)
# Display the matrix
print("Matrix:")
print(mat)
# Compute row-wise sums
row_sums <- rowSums(mat)
# Print row sums
print("Row Sums:")
print(row_sums)
Output:
[1] "Matrix:"
[,1] [,2] [,3]
[1,] 3 6 9
[2,] 4 7 10
[3,] 5 8 11
[1] "Row Sums:"
[1] 18 21 24
Compute the Sum of Rows of an Array in R
Now, let’s create a 3D array and use rowSums() to calculate row-wise summation.
Example:
# Creating a 3D array
arr <- array(1:12, dim = c(2, 3, 2))
# Display the array
print("Array:")
print(arr)
# Compute row-wise sums across specified dimensions
row_sums <- rowSums(arr, dims = 1)
# Print row sums
print("Row Sums:")
print(row_sums)
Output:
[1] "Array:"
,,1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
,,2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
[1] "Row Sums:"
[1] 42 48
Compute the Sum of Rows of a Data Frame in R
Now, let’s compute the row sums of a data frame with numerical values.
Example:
# Creating a data frame
df <- data.frame(
ID = c(101, 102, 103),
Score1 = c(12, 18, 24),
Score2 = c(8, 14, 22)
)
# Display the data frame
print("Data Frame:")
print(df)
# Compute row-wise sums
row_sums <- rowSums(df[, c("Score1", "Score2")])
# Print row sums
print("Row Sums:")
print(row_sums)
Output:
[1] "Data Frame:"
ID Score1 Score2
1 101 12 8
2 102 18 14
3 103 24 22
[1] "Row Sums:"
[1] 20 32 46
Use rowSums() with NA Values in a Data Frame
Now, let’s create a dataset containing missing values and compute row sums while treating NA values as zero.
Example:
# Creating a data frame with missing values
df_na <- data.frame(
ID = c(201, 202, 203),
Score1 = c(10, NA, 28),
Score2 = c(6, 14, NA)
)
# Display the data frame with missing values
print("Data Frame with Missing Values:")
print(df_na)
# Compute row-wise sums while ignoring NA values
row_sums <- rowSums(df_na[, c("Score1", "Score2")], na.rm = TRUE)
# Print row sums
print("Row Sums:")
print(row_sums)
Output:
[1] "Data Frame with Missing Values:"
ID Score1 Score2
1 201 10 6
2 202 NA 14
3 203 28 NA
[1] "Row Sums:"
[1] 16 14 28
We use the argument na.rm = TRUE in rowSums() to treat missing values as zero during summation.
Use rowSums() with Specific Columns in a Data Frame
We can also select specific columns from a data frame and compute their row-wise sum.
Example:
# Creating a data frame with missing values
df_selected <- data.frame(
ID = c(301, 302, 303),
Exam1 = c(15, NA, 26),
Exam2 = c(9, 17, NA),
Exam3 = c(NA, 20, 7),
Exam4 = c(18, 25, NA)
)
# Display the original data frame
print("Data Frame with Missing Values:")
print(df_selected)
# Compute row-wise sums for specific columns while ignoring NA values
row_sums <- rowSums(df_selected[, c("Exam2", "Exam4")], na.rm = TRUE)
# Print row sums
print("Row Sums:")
print(row_sums)
Output:
[1] "Data Frame with Missing Values:"
ID Exam1 Exam2 Exam3 Exam4
1 301 15 9 NA 18
2 302 NA 17 20 25
3 303 26 NA 7 NA
[1] "Row Sums:"
[1] 27 42 0
Leave a Reply