0

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.

1 Answer 1

1

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_)
  )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.