Jakhongir Alidjanov
Jakhongir Alidjanov

Reputation: 109

dplyr conditional sum of columns containing specific value

I have faced a problem of mutating the conditional calculation of specific columns, containing specific values. The algorythm I want to code is: if condition1=x, calculate the number of columns that contain "a" among columns with "bbb" in the title , if condition1=y, calculate the number of columns that contain "c" among columns with "ddd" in the title. As an example, I give the following:

require("tidyverse")
iris %>% 
  mutate_all(as.character) %>% 
  select(Species, everything()) %>% 
  rowwise() %>% 
  mutate(cat1=case_when(Species=="virginica"~sum(select(., contains("sepal"), endsWith("5"))),
                        Species=="versicolor"~sum(select(., contains("sepal"), startsWith("6"))),
                        TRUE~"not tested"))

Could you please give me your advices? Thank you all in advance.

Upvotes: 0

Views: 988

Answers (1)

lhs
lhs

Reputation: 1038

The c_across() function will get you what you need. I also changed the final case in the case_when() to return an integer because the others are integers, and they all must be the same type.

iris %>% 
  mutate_all(as.character) %>% 
  select(Species, everything()) %>% 
  rowwise() %>% 
  mutate(cat1 = case_when(
    Species == "virginica" ~ sum(endsWith(c_across(contains("sepal")), "5")),
    Species == "versicolor" ~ sum(endsWith(c_across(contains("sepal")), "6")),
    TRUE ~ NA_integer_)
  )

Upvotes: 1

Related Questions