简体   繁体   English

在 r 中处理 if_else 中的 NA

[英]handling NA in if_else in r

I have the following dataset with three columns containing dates.我有以下包含日期的三列数据集。

library(dplyr)

set.seed(45)

df1 <- data.frame(hire_date = sample(seq(as.Date('1999/01/01'),    as.Date('2000/01/01'), by="week"), 10),
              t1 = sample(seq(as.Date('2000/01/01'), as.Date('2001/01/01'), by="week"), 10),
              t2 = sample(seq(as.Date('2000/01/01'), as.Date('2001/01/01'), by="day"), 10))

#this value is actually unknown
df1[10,2] <- NA

    hire_date         t1         t2
1  1999-08-20 2000-05-13 2000-02-17   
2  1999-04-23 2000-11-11 2000-04-27   
3  1999-03-26 2000-04-15 2000-08-01   
4  1999-05-07 2000-06-03 2000-08-29   
5  1999-04-30 2000-05-27 2000-11-19   
6  1999-04-09 2000-12-30 2000-01-26   
7  1999-03-12 2000-12-23 2000-12-07  
8  1999-06-25 2000-02-12 2000-09-26  
9  1999-02-26 2000-05-06 2000-08-23 
10 1999-01-01       <NA> 2000-03-18 

I'd like to perform an if else statement such that df1$com is 1 if the difference between t1 OR t2 and hire_date is between [395,500]如果 t1 OR t2 和hiring_date 之间的差在 [395,500] 之间,我想执行一个 if else 语句,使 df1$com 为 1

The following if_else statement almost gets me there, but the NA mucks it up.下面的 if_else 语句几乎让我明白了,但 NA 把它搞砸了。 Any ideas?有任何想法吗?

df1$com <- if_else((df1$t1 - df1$hire_date) >= 395 &
               (df1$t1 - df1$hire_date) <= 500, 1,
       if_else((df1$t2 - df1$hire_date) >= 395 &
                (df1$t2 - df1$hire_date) <= 500, 1, 0))

You could use dplyr::case_when instead of nesting the if_else statements.您可以使用dplyr::case_when而不是嵌套if_else语句。 It will give you easy control over how to treat NA .它将让您轻松控制如何治疗NA And dplyr::between will clean things up as well for your date comparisons.并且dplyr::between也会为您的日期比较进行清理。

df1 %>%
  mutate(com = case_when(
    is.na(t1) | is.na(t2) ~ 999, # or however you want to treat NA cases
    between(t1 - hire_date, 395, 500) ~ 1,
    between(t2 - hire_date, 395, 500) ~ 1,
    TRUE ~ 0 # neither range is between 395 and 500
  ))

#>     hire_date         t1         t2 com
#> 1  1999-08-20 2000-05-13 2000-02-17   0
#> 2  1999-04-23 2000-11-11 2000-04-27   0
#> 3  1999-03-26 2000-04-15 2000-08-01   1
#> 4  1999-05-07 2000-06-03 2000-08-29   1
#> 5  1999-04-30 2000-05-27 2000-11-19   0
#> 6  1999-04-09 2000-12-30 2000-01-26   0
#> 7  1999-03-12 2000-12-23 2000-12-07   0
#> 8  1999-06-25 2000-02-12 2000-09-26   1
#> 9  1999-02-26 2000-05-06 2000-08-23   1
#> 10 1999-01-01       <NA> 2000-03-18 999

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM