Home > OS >  How to use dplyr's if_all() within case_when with numeric data in R?
How to use dplyr's if_all() within case_when with numeric data in R?

Time:10-19

I have my data organized into classes, and if all of the scores1 values are above 100, then I want their true_score to be score1. However, if anyone in the class has a score1 value below 100, then I want to add their score1 and score2 values to be their true score.

I've tried and would like to use dplyr's if_all(), but I can't get it to work.

Here's my mock data:

library(tidyverse)

test <- tibble(person = c("c", "s", "j"),
               class = c(1, 2, 2),
               score1 = c(101, 200, 23),
               score2 = c(200, 100, 25))

Here's what I want:


answer <- tibble(person = c("c", "s", "j"),
                 class = c(1, 2, 2),
                 score1 = c(101, 200, 23),
                 score2 = c(200, 100, 25),
                 true_score = c(101, 300, 48))

And here's my (failed) attempt:

test %>%
  group_by(class) %>%
  mutate(true_score = case_when(
    if_all(score1 > 100), score1 > 100 ~ score1,
    score1   score2 > 100 ~ score1   score2
  ))

Error in `mutate()`:
! Problem while computing `true_score = case_when(...)`.
ℹ The error occurred in group 1: class = 1.
Caused by error in `if_all()`:
! object 'score1' not found

CodePudding user response:

if_all() (and if_any()) are for use across columns rather than within. In this case, you want plain old all():

library(dplyr)

test %>%
  group_by(class) %>%
  mutate(true_score = case_when(all(score1 > 100) ~ score1,
                                TRUE ~ score1   score2)) %>%
  ungroup()

# A tibble: 3 × 5
  person class score1 score2 true_score
  <chr>  <dbl>  <dbl>  <dbl>      <dbl>
1 c          1    101    200        101
2 s          2    200    100        300
3 j          2     23     25         48
  • Related