mbeasle2
mbeasle2

Reputation: 163

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!

Upvotes: 0

Views: 738

Answers (1)

Joel Kandiah
Joel Kandiah

Reputation: 1525

Fortunately this issue is relatively trivial. In case_when() we check through a list of conditions in order, so if the second argument had the condition TRUE and we set if this condition is met the result to be carb we could attain our result.

I have produced an illustrative reprex below.

library(tidyverse)

mtcars %>%
  mutate(carb = case_when(
    cyl == 4 ~ 5000,
    TRUE ~ carb
  )) %>% 
  head(10)
#>                    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4 5000
#> Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#> Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
#> Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4 5000
#> Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4 5000
#> Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4

Created on 2021-04-07 by the reprex package (v2.0.0)

Upvotes: 2

Related Questions