Home > Back-end >  case_when() in R returns a larger length vector than is expected
case_when() in R returns a larger length vector than is expected

Time:01-27

Why does case when return a larger length vector when the evaluated condition only is length one?

This is using the dplyr::case_when().

Here is my example:

g <- list("something", c(1:10))

case_when( g[[1]] == "not something" ~ sum(g[[2]]), 
           g[[1]] == "something" ~ g[[2]][3], 
           TRUE ~ g[[2]]
)

I would've expected the result to be 3 instead of

3 3 3 3 3 3 3 3 3 3 3

Edit: Reviewing the documentation for the function perhaps the above should return an error instead of running?

Value
A vector of length 1 or n, matching the length of the logical input or output vectors, with the type (and attributes) of the first RHS. Inconsistent lengths or types will generate an error..

CodePudding user response:

You issue comes from the last condition TRUE ~ g[[2]] that returns a vector. Other conditions returns a single value and that confuses case_when.

You can achieve what you want by unifying the output to the list.

case_when( g[[1]] == "not something" ~ list(sum(g[[2]])), 
       g[[1]] == "something" ~ list(g[[2]][3]), 
       TRUE ~ g[2]
)
  • Related