如何在 R 中跨多列查找最早日期(NAs 问题)

How to find the earliest date across multiple columns in R (Issue with NAs)

我有 3 个日期列(class-日期),我想创建一个包含 3 个日期中最早日期的新列。这是我在下面使用的代码:

df1 <- df %>% mutate(timeout= pmin(date1, date2, end_date))

如果 date1date2NA,那么我希望 end_date 中的日期在 timeout 中返回列,因此 timeout 不应有任何 NA。上面的代码带回 NAs。如有任何帮助,我们将不胜感激。

可以加上na.rm = TRUE,那么在计算pmin的时候会忽略每一行的NA

library(dplyr)

df %>% 
  mutate(timeout = pmin(date1, date2, end_date, na.rm = TRUE))

输出

  id      date1      date2   end_date    timeout
1  1       <NA>       <NA> 2008-01-23 2008-01-23
2  1 2007-10-16 2007-11-01 2008-01-23 2007-10-16
3  2 2007-11-30 2007-11-30 2007-11-30 2007-11-30
4  3 2007-08-17 2007-12-17 2008-12-12 2007-08-17
5  3 2008-11-12 2008-12-12 2008-12-12 2008-11-12

数据

df <- structure(list(id = c(1L, 1L, 2L, 3L, 3L), date1 = structure(c(NA, 
13802, 13847, 13742, 14195), class = "Date"), date2 = structure(c(NA, 
13818, 13847, 13864, 14225), class = "Date"), end_date = c("2008-01-23", 
"2008-01-23", "2007-11-30", "2008-12-12", "2008-12-12")), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5"))