Home > Enterprise >  Calculate the sum of all column values in a dataframe
Calculate the sum of all column values in a dataframe

Time:08-25

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

  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?

CodePudding user response:

Try this:

MaleDF1 <- sum(MaleDF)

CodePudding user response:

The accepted answer will not work if there is a non-numeric column in the dataframe. Instead, you can do:

num_columns = lapply(MaleDF, is.numeric) |>  # check which columns are numeric
        unlist(use.names = FALSE) # return a vector

sum(MaleDF[, num_columns]) # select the numeric columns while computing sum

Alternatively you can use dplyr:

library(dplyr)

MaleDF |> 
        select(where(is.numeric)) |> 
        sum()

CodePudding user response:

If you have non-numeric columns as your question suggests, you can use this to avoid errors:

sum(Filter(is.numeric, MaleDF))
#[1] 198
  • Related