使用R从多个日期列中查找最小和最大日期

sczxawaw  于 2023-05-20  发布在  其他
关注(0)|答案(2)|浏览(138)

我想从一个日期列列表中创建一个名为maxdate和mindate的新列,假设有4个日期列,并且有缺失值。
下面的解决方案仅给出列的行的最大值/最小值。我感兴趣的是在列中查找最大/最小日期。

df$maxdate <- apply (df[1:4], 1, max, na.rm = TRUE)
df <- data.frame(
  col1 = c("11/09/1999", "11/09/1999", "11/09/1999", "11/09/1999", "11/09/1999"),
  col2 = c("01/01/2000", "01/01/2000", "01/01/2000", "01/01/2000", "01/01/2000"),
  col3 = c("12/09/1961", "10/03/1995", "30/03/1992", "25/05/1992", "25/05/1992"),
  col4 = c("01/01/1930", "01/01/1939", "01/01/1942", "01/01/1936", "01/01/1937")
)

样本数据

col1          col2        col3      col4

11/09/1999  01/01/2000  12/09/1961  01/01/1930
11/09/1999  01/01/2000  10/03/1995  01/01/1939
11/09/1999  01/01/2000  30/03/1992  01/01/1942
11/09/1999  01/01/2000  25/05/1992  01/01/1936
11/09/1999  01/01/2000  25/05/1992  01/01/1937
5w9g7ksd

5w9g7ksd1#

像这样?

df <- data.frame(
  col1 = c("11/09/1999", "11/09/1999", "11/09/1999", "11/09/1999", "11/09/1999"),
  col2 = c("01/01/2000", "01/01/2000", "01/01/2000", "01/01/2000", "01/01/2000"),
  col3 = c("12/09/1961", "10/03/1995", "30/03/1992", "25/05/1992", "25/05/1992"),
  col4 = c("01/01/1930", "01/01/1939", "01/01/1942", "01/01/1936", "01/01/1937")
)

maxdate <- lapply(df, function (x) max(x, na.rm = TRUE))



> maxdate
$col1
[1] "11/09/1999"

$col2
[1] "01/01/2000"

$col3
[1] "30/03/1992"

$col4
[1] "01/01/1942"

请注意,我没有将其分配给列作为行数!=列数,因此无法将其添加到 Dataframe 中。

vaqhlq81

vaqhlq812#

library(dplyr)

df = data.frame(date1 = c("2023-05-11", "2023-04-12","2023-07-13","2023-01-14","2023-05-15"),
                date2 = c("2023-04-11", "2023-07-12","2023-09-13","2023-05-14","2023-12-15"),
                date3 = c("2023-08-11", "2023-06-12","2023-08-13","2023-08-14","2023-05-15"),
                date4 = c("2023-01-11", "2023-05-12","2023-05-13","2023-12-14","2023-05-15"))

df <- df  %>% mutate_all(as.Date)

# edit: removed rowwise and added na.rm=TRUE, as you seem to want the max from all rows, disregarding NAs?
df <- df %>%  mutate(max_date = max(date1, date2,date3,date4, na.rm=TRUE))

df

相关问题