2021年4月11日星期日

case_when() mutate in pipe but maintain original data

I have a df that I created a column on based on outside data. I have about 4 values that did not have a match in outside data, so they have 'NA' in my column. I want to fix the NAs manually using mutate() and case_when() but I cannot figure out how to tell my case_when() to maintain the data that is not starting as 'NA'. Example data below:

data(mtcars)  
mtcars %>%    mutate(carb = case_when(      cyl == 4 ~ 5000    ))  
cyl    carb  <dbl> <dbl>    6   NA            6   NA            4   5000              6   NA            8   NA            6   NA            8   NA            4   5000              4   5000              6   NA    

If this were my data, I would want the 5000 along with the original data in the 'carb' column.

Thank you!

https://stackoverflow.com/questions/66994792/case-when-mutate-in-pipe-but-maintain-original-data April 08, 2021 at 06:10AM

没有评论:

发表评论