adl
adl

Reputation: 1441

r - apply single condition on multiple columns

I have a dataframe with many columns and getting the following summary is lots of manual work. In the sample dataset it is required to calculate the sum of the value column three times only for the observations where the con columns are "TRUE" separately for each.

# sample dataset
df <- data.frame(
  value = c(1:20),
  con1 = c("TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE"),
  con2 = c("FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE"),
  con3 = c("FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "TRUE", "FALSE", "FALSE", "FALSE"))

Manually the process can be done like this:

df %>% group_by(con1) %>% summarise(value = sum(value))

In this case the sum of the value column for the observations only when the corresponding value in the con1 column is "TRUE", is 70.

The end dataframe should look like this:

data.frame(con1 = 70, con2 = 63, con3 = 57)  

Upvotes: 0

Views: 548

Answers (1)

akuiper
akuiper

Reputation: 214927

You can use summarise_at to summarize con columns, and for each column, apply it as logical filter to value column and sum it up:

df %>% 
    summarise_at(vars(starts_with('con')), funs(sum(value[as.logical(.)])))

#  con1 con2 con3
#1   70   63   57

Upvotes: 2

Related Questions