# Assuming 'your_data' is your data frame
library(dplyr)
# Step 1: Use add_row() function from dplyr to add a total row
your_data <- your_data %>%
add_row(Total = "Total")
# Step 2: Use summarise() to calculate the sum for each numeric column
your_data <- your_data %>%
summarise(across(where(is.numeric), sum, na.rm = TRUE))
# Step 3: Transpose the result to get a row with total values
your_data <- t(your_data)
# Step 4: Assign column names to the result
colnames(your_data) <- "Total"
# Step 5: Bind the total row to the original data frame
your_data <- bind_rows(your_data, your_data, .id = "Group")
# Optional: If needed, you can replace NAs with 0 in the total row
your_data[is.na(your_data)] <- 0