我正在尝试查找由分隔符分隔的列中的最早日期(“ “在本例中),并创建一个新列。
data.frame(x = c("2023-1-2\n2034-02-10", "2023-1-2\n2001-10-30")) %>%
mutate(earliest_date = sapply(strsplit(x, "\\\n"),
function(x) min(parse_date_time(x, orders = c("mdy", "ymd")), na.rm = T)))
当我运行这个时,它似乎会产生正确的答案,但在几秒钟内:
x earliest_date
1 2023-1-2\n2034-02-10 1672617600
2 2023-1-2\n2001-10-30 1004400000
我如何收到日期格式的正确日期?
编辑:这似乎可以解决问题,但我如何将其添加为列?
lapply(strsplit(c("2023-1-2\n2034-02-10", "2023-1-2\n2001-10-30"), "\\\n"),
function(x) min(parse_date_time(x, orders = c("mdy", "ymd")), na.rm = T))
在 R 基础上你可以做
df$earliest_date <- unlist(lapply(strsplit(df$x, "\\\n"), min))
输出:
x earliest_date
1 2023-1-2\n2034-02-10 2023-1-2
2 2023-1-2\n2001-10-30 2001-10-30