Created
July 22, 2021 12:32
-
-
Save daltonjorge/cf95c8e0b1fe9518776cc3332c423496 to your computer and use it in GitHub Desktop.
Add a total row to a R data frame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Solution 1 | |
original_data_frame <- data.frame( | |
name = c('John','Paul','Mary'), | |
age = c(25, 30, 20), | |
salary = c(90000, 110000,85000) | |
) | |
new_df_with_totals <- data.frame( | |
name = 'Total', | |
colSums(t(original_data_frame[,-1])) | |
) | |
rbind(original_data_frame, new_df_with_totals) | |
% Solution 2 | |
new_df <- original_data_frame | |
new_df[4,] = c("Total", colSums(new_df[,2:3])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment