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.