Size Error: Ifelse vs. if_else + diff

Greetings!
I ran into a strange issue with if_else within group_modify today. This very minimal example does away with the group_modify part and just uses summarize.

It seems clear that if_else is checking both cases regardless of which case is actually executed while ifelse truly only evaluates the T/F expression when conditions are satisfied, so what are the alternatives within the tidyverse to handle this?

For what it's worth, I experimented with different applications of n(), nrow(.), etc., and if_else remained unsuccessful. Also experimented with map(), but map does not appear to respect grouping.

library(dplyr)

example <- tibble(
  Group = c("A", "A", "B"),
  Name = c("Jane Doe", "Fizz Buzz", "John Smith"),
  Score = c(100, 89, 76)
)

example %>% 
  group_by(Group) %>% 
  summarize(
    count = n(),
    difference = ifelse(count != 2, NA, diff(Score))
  )
#  Group count difference
#  <chr> <int>      <dbl>
# 1 A         2        -11
# 2 B         1         NA

example %>% 
  group_by(Group) %>% 
  summarize(
    count = n(),
    difference = if_else(count != 2, NA, diff(Score))
  )
# Error in `summarize()`:
# ℹ In argument: `difference = if_else(count != 2, NA, diff(Score))`.
# ℹ In group 2: `Group = "B"`.
# Caused by error in `if_else()`:
# ! `false` must have size 1, not size 0.

I found this on GitHub addressing it in a general sense but not within the context of tidy evaluation.
`if_else` being strict about object length · Issue #6879 · tidyverse/dplyr · GitHub
Thank you for any clarification or discussion!

As in the Github issue, you are not really using the vectorized aspect of if_else, you are returning a single value. I believe summarize will complain if you try to return multiple values. You can just use a simple if().

example %>% 
  group_by(Group) %>% 
  summarize(
    count = n(),
    difference = if(count != 2) NA else diff(Score)
  )
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.