andy
andy

Reputation: 2077

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

  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?

Upvotes: 2

Views: 245

Answers (3)

bird
bird

Reputation: 3316

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()

Upvotes: 1

Ma&#235;l
Ma&#235;l

Reputation: 52319

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

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

Upvotes: 0

XixiHaha
XixiHaha

Reputation: 173

Try this:

MaleDF1 <- sum(MaleDF)

Upvotes: 1

Related Questions