Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculate the sum of all column values in a dataframe

I have a data frame that has over 50 columns. I have selected columns with a specific string "Male" and this is the hypothetical result of the DF

Male_under_18years <- c(12, 23, 45,45)
Male_btn_19_and_25years <- c(5, 6,7, 10)
male_over_25_years <- c(4, 20, 20, 1)
MaleDF <- data.frame(Male_under_18years, Male_btn_19_and_25years, male_over_25_years)

I’d like to get the total number of all Male persons from the dataframe and display the value in a valueBox in shinydashboard. So far, I’ve been able to get the total number of all males under each column using

MaleDF <- MaleDF %>%
  summarise(across(where(is.numeric), ~sum(.x, na.rm = T)))

Which gives the output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  Male_under_18years   Male_btn_19_and_25years   male_over_25_years
                125                      28                 45

However, I’d like a code that autamitcally calculates the total values in each of the column to give me the total number which is 198

How do I go about that?

>Solution :

Try this:

MaleDF1 <- sum(MaleDF)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading