简体   繁体   English

将日期列合二为一

[英]Combine date columns into one

I want to combine date columns by the latest date per row (if it is different) but with keeping the ID column.我想按每行的最新日期(如果不同)组合日期列,但保留 ID 列。 My data frame looks like the image.我的数据框看起来像图像。 I want to keep the NA rows.我想保留 NA 行。 As you can see in some rows the timestamp_c is filled and not the timestamp (in some others, it is the opposite).正如您在某些行中看到的那样,timestamp_c 被填充而不是时间戳(在其他一些行中,它是相反的)。 I want to keep the column which is completed and not the NA.我想保留已完成的列而不是 NA。 I tried to follow this but I could not find a solution我试图遵循这个,但我找不到解决方案

在此处输入图片说明

library(data.table)

df <- data.table(
  ID = LETTERS[1:7],
  timestamp_c = lubridate::ymd("2021-03-08", NA, NA, "2021-04-06", NA, "2021-04-06", "2021-04-07"),
  timestamp = lubridate::ymd(NA, NA, NA, "2021-04-06", "2021-05-05", "2021-04-07", "2021-04-06")
)

df[, new_timestamp := max(timestamp_c, timestamp, na.rm = TRUE), by = ID]

#    ID timestamp_c  timestamp new_timestamp
# 1:  A  2021-03-08       <NA>    2021-03-08
# 2:  B        <NA>       <NA>          <NA>
# 3:  C        <NA>       <NA>          <NA>
# 4:  D  2021-04-06 2021-04-06    2021-04-06
# 5:  E        <NA> 2021-05-05    2021-05-05
# 6:  F  2021-04-06 2021-04-07    2021-04-07
# 7:  G  2021-04-07 2021-04-06    2021-04-07

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

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