简体   繁体   中英

case_when() in R returns a larger length vector than is expected

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..

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]
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM