I have a problem understanding how dplyr::case_when
works. Here with this pretty simple line :
library(tidyverse)
case_when(TRUE ~ 50,
FALSE ~ numeric(0))
I get numeric(0)
while obviously, TRUE is TRUE and so it should send back 50. Besides, FALSE is FALSE so it should never send back numeric(0). I have not the problem if I write :
case_when(TRUE ~ 50,
FALSE ~ NaN)
Where I get 50, which is right. What do I miss ?
dplyr
team on github. Generally, every outcome ofcase_when
should have the same type and the same length. For example,case_when(TRUE ~ 1:3, FALSE ~ 1:2)
throws an error. – Concettaconcettinacase_when
evaluating things it doesn't need to. I'd forego length checking for efficiency.case_when(TRUE ~ 1, FALSE ~ {Sys.sleep(10); 0})
takes 10 seconds to return, but it could be instant. – Gauntlettif_else
andcase_when
are not short-circuited, @GregorThomas; while I agree that it would be a great thing, I don't think it's in the cards to make it so. :-( – Problematicif_else
did to improve performance overifelse
, butbase::ifelse(TRUE, 1, {Sys.sleep(10); 0})
actually is short-circuited! – Gauntlett</rant>
. Unlikely to change in base R, unfortunately. Butdplyr
makes intentional effort on things similar to this (enforcingclass
, e.g., whenifelse
does not), surprised about this. – Problematicx <- 1:-1; case_when(x > 0 ~ log(x), TRUE ~ as.numeric(x))
. – Gauntlettfcase
warns, too ... and it does no recycling (a problem in my book), soTRUE
would need to berep(TRUE,3)
here (c.f., github.com/Rdatatable/data.table/issues/4258, still open). – Problematic