The Great
The Great

Reputation: 7723

Assign a custom number while mutating and replacing NA using R

I have a dataframe like as shown below

id            date      from       to         type  
  <chr>      <date>     <date>     <date>     <chr>          
 1          2011-02-21 2011-01-01 2011-12-31 NA               
 2          2013-03-11 2013-01-01 2013-12-31 NA               
 3          2015-12-31 2015-01-01 2015-12-31 last             
 4          NA         NA         NA         NA                
 5          NA         NA         NA         NA  

I would like to calculate the range that we can shift the days backward or forward.

I wrote the below code and it works fine for Non-NA scenarios as shown below

     data %>%
        mutate(shift_back_max = (date - from) / ddays(1),
                 shift_forward_max = (to - date) / ddays(1)) %>%
        data[c("shift_back_max", "shift_forward_max")][is.na(data[c("shift_back_max", "shift_forward_max")])] <- 30   # error is here with the way I am replacing `NA` with 30

enter image description here

I guess the problem I am facing is replacing NA during a %>% operation task.

For NA scenarios, I would like to assign a custom range of 30-30.

Either you can replace NA with 30 (for those two columns only) or any other approach to key in 30 for NA is also fine

I expect my output to be like as shown below

identifier    date       from       to       type  shift_back_max shift_forward_max
                                            
1          2011-02-21 2011-01-01 2011-12-31 NA                51               313
2          2013-03-11 2013-01-01 2013-12-31 NA                69               295
3          2015-12-31 2015-01-01 2015-12-31 last             364                 0
4          NA         NA         NA         NA                30                30
5          NA         NA         NA         NA                30                30

Upvotes: 0

Views: 85

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389047

You can replace NA with 30 in mutate for those two columns :

library(dplyr)
library(lubridate)

df %>%
  mutate(shift_back_max = (date - from) / ddays(1),
         shift_forward_max = (to - date) / ddays(1), 
         across(starts_with('shift'), tidyr::replace_na, 30))

#  id       date       from         to type shift_back_max shift_forward_max
#1  1 2011-02-21 2011-01-01 2011-12-31 <NA>             51               313
#2  2 2013-03-11 2013-01-01 2013-12-31 <NA>             69               295
#3  3 2015-12-31 2015-01-01 2015-12-31 last            364                 0
#4  4       <NA>       <NA>       <NA> <NA>             30                30
#5  5       <NA>       <NA>       <NA> <NA>             30                30

Upvotes: 1

Related Questions