Avoiding type conflicts with dplyr::case_when

后端 未结 2 949

I am trying to use dplyr::case_when within dplyr::mutate to create a new variable where I set some values to missing and recode other values simult

2条回答
  •  既然无缘
    2020-12-13 18:53

    Try this ?

    df %>% dplyr::mutate(new = dplyr::case_when(.$old == 1 ~ 5,
                                                      .$old == 2 ~ NA_real_,
                                                      TRUE~.$old))
    
    > df
      old new
    1   1   5
    2   2  NA
    3   3   3
    

提交回复
热议问题