Home > Back-end >  R: replace NA with fixed date in date column variable (if condition holds)
R: replace NA with fixed date in date column variable (if condition holds)

Time:09-26

I would like to replace NA values in a given column (in date format) with a fixed date, if a given condition hold.

In the example below I would like to replace NA values in dt column with "2012-07-01" if condition a==4 holds. I am looking for the simplest way of doing so, either in dplyr or data.table.

> df1 <- data.table(a = c(1, 2, 3, 4),
                   dt = as.Date(c("2012-06-01", "2012-07-01", NA, NA)))
> df1
   a         dt
1: 1 2012-06-01
2: 2 2012-07-01
3: 3       <NA>
4: 4       <NA>

CodePudding user response:

With data.table

df1[a == 4 & is.na(dt), dt := as.Date("2012-07-01")]
       a         dt
   <num>     <Date>
1:     1 2012-06-01
2:     2 2012-07-01
3:     3       <NA>
4:     4 2012-07-01

In Base R for completeness:

df1[df1$a == 4 & is.na(df1$dt), "dt"] <- "2012-07-01"

CodePudding user response:

library(tidyverse)
df1 %>% 
    mutate(dt = case_when(a == 4 ~ as.Date("2012-07-01"), 
                          TRUE ~ dt))

# A tibble: 4 × 2
      a dt        
  <dbl> <date>    
1     1 2012-06-01
2     2 2012-07-01
3     3 NA        
4     4 2012-07-01

CodePudding user response:

With dplyr and lubridate (so i can use ymd instead of as.Date):

library(data.table)
library(dplyr)
library(lubridate)

df1 <- data.table(a = c(1, 2, 3, 4),dt = as.Date(c("2012-06-01", "2012-07-01", NA, NA)))
df1%>%
  mutate(dt=if_else(is.na(dt) & a==4,
                    ymd("2012-07-01"),dt))
  • Related