简体   繁体   中英

Row-wise min() on right hand when using dplyr::case_when

I am trying to fetch the smaller of one variable at one observation and a fixed number in a conditional dplyr::case_when statement in R. But the min() statement compares to the smallest observation of the entire variable vector, ie not for each observation.

library(tidyverse)
data_frame(spc = rep(c("cat", "dog"), 3), z = 1:6) %>%
   mutate(
    dogsmax3 = case_when(
      spc == "dog" ~ min(z, 3),
      TRUE ~ 0))
 #  spc       z dogsmax3
 #  <chr> <int>    <dbl>
 #1 cat       1        0
 #2 dog       2        1
 #3 cat       3        0
 #4 dog       4        1
 #5 cat       5        0
 #6 dog       6        1

While I look for a statement making this result:

 #  spc       z dogsmax3
 #  <chr> <int>    <dbl>
 #1 cat       1        0
 #2 dog       2        2
 #3 cat       3        0
 #4 dog       4        3
 #5 cat       5        0
 #6 dog       6        3

So, any suggestions for a better apprach?

Use rowwise . Instead of case_when you could also use an ifelse :

library(tidyverse)
data_frame(spc = rep(c("cat", "dog"), 3), z = 1:6) %>%
   rowwise() %>%
   mutate(dogsmax3 = ifelse(spc == "dog", min(z,3), 0)) %>%
   ungroup() ## to revert the grouping by rows

You can use ?pmin instead of min -

data_frame(spc = rep(c("cat", "dog"), 3), z = 1:6) %>%
  mutate(
    dogsmax3 = case_when(
      spc == "dog" ~ pmin(z, 3),
      TRUE ~ 0)
  )

# A tibble: 6 x 3
  spc       z dogsmax3
  <chr> <int>    <dbl>
1 cat       1     0   
2 dog       2     2.00
3 cat       3     0   
4 dog       4     3.00
5 cat       5     0   
6 dog       6     3.00

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-2024 STACKOOM.COM