将多个日期和时间列合并到datetime列R中

时间:2019-03-14 03:17:53

标签: r dplyr data.table

对于以下数据:

new

我想将> dt date event1 event2 event3 1: 2016-04-27 10:25:15 11:05:45 13:00:09 2: 2016-04-27 10:25:15 11:05:45 13:00:09 3: 2016-04-27 10:25:15 11:05:45 13:00:09 4: 2016-04-27 10:25:15 11:05:45 13:00:09 5: 2016-04-27 10:25:15 11:05:45 13:00:09 与每个事件列合并,以使事件时间列成为date格式。所需的输出:

datetime

由于我有一个很大的数据集,包含30万行和20多个事件时间列,因此dt$event1 = as.POSIXct(paste(dt$date, dt$event1), format="%Y-%m-%d %H:%M:%S") dt$event2 = as.POSIXct(paste(dt$date, dt$event2), format="%Y-%m-%d %H:%M:%S") dt$event3 = as.POSIXct(paste(dt$date, dt$event3), format="%Y-%m-%d %H:%M:%S") dt$date = NULL > dt event1 event2 event3 1: 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09 2: 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09 3: 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09 4: 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09 5: 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09 中所有事件时间列一次执行此操作的最有效方式是什么还是dplyr

样本数据:

data.table

3 个答案:

答案 0 :(得分:2)

不确定合并与这有什么关系;这不只是

dt[, event1_datetime := as.POSIXct(paste(date, event1))]
#         date   event1   event2   event3     event1_datetime
#1: 2016-04-27 10:25:15 11:05:45 13:00:09 2016-04-27 10:25:15
#2: 2016-04-27 10:25:15 11:05:45 13:00:09 2016-04-27 10:25:15
#3: 2016-04-27 10:25:15 11:05:45 13:00:09 2016-04-27 10:25:15
#4: 2016-04-27 10:25:15 11:05:45 13:00:09 2016-04-27 10:25:15
#5: 2016-04-27 10:25:15 11:05:45 13:00:09 2016-04-27 10:25:15

更新

对于它的价值,这是一个使用data.tablemelt的{​​{1}}解决方案

dcast

或者一劳永逸

dt[, n := 1:.N]
dt <- melt(dt[, n := 1:.N], id.vars = c("date", "n"), value.name = "time")
dt[, datetime := as.POSIXct(paste(date, time))]
dt <- dcast(dt, date + n ~ variable, value.var = c("time", "datetime"))
dt[, n := NULL]
#         date time_event1 time_event2 time_event3     datetime_event1
#1: 2016-04-27    10:25:15    11:05:45    13:00:09 2016-04-27 10:25:15
#2: 2016-04-27    10:25:15    11:05:45    13:00:09 2016-04-27 10:25:15
#3: 2016-04-27    10:25:15    11:05:45    13:00:09 2016-04-27 10:25:15
#4: 2016-04-27    10:25:15    11:05:45    13:00:09 2016-04-27 10:25:15
#5: 2016-04-27    10:25:15    11:05:45    13:00:09 2016-04-27 10:25:15
#       datetime_event2     datetime_event3
#1: 2016-04-27 11:05:45 2016-04-27 13:00:09
#2: 2016-04-27 11:05:45 2016-04-27 13:00:09
#3: 2016-04-27 11:05:45 2016-04-27 13:00:09
#4: 2016-04-27 11:05:45 2016-04-27 13:00:09
#5: 2016-04-27 11:05:45 2016-04-27 13:00:09

答案 1 :(得分:1)

我们可以使用mutate_at添加新列

library(dplyr)

dt %>%
  mutate_at(vars(starts_with("event")), funs(as.POSIXct(paste0(date, .)))) %>%
  select(-date)

#               event1              event2              event3
#1 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09
#2 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09
#3 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09
#4 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09
#5 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09

答案 2 :(得分:1)

使用.SDcols的可能方法:

cols <- paste0(grep("^event", names(dt), value=TRUE), "_datetime")
dt[, (cols) := 
    lapply(.SD, function(x) as.POSIXct(paste(date, x), format="%Y-%m-%d %H:%M:%S")), 
        .SDcols=event1:event3]

输出:

         date   event1   event2   event3     event1_datetime     event2_datetime     event3_datetime
1: 2016-04-27 10:25:15 11:05:45 13:00:09 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09
2: 2016-04-27 10:25:15 11:05:45 13:00:09 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09
3: 2016-04-27 10:25:15 11:05:45 13:00:09 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09
4: 2016-04-27 10:25:15 11:05:45 13:00:09 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09
5: 2016-04-27 10:25:15 11:05:45 13:00:09 2016-04-27 10:25:15 2016-04-27 11:05:45 2016-04-27 13:00:09

数据:

library(data.table)
dt <- fread("date   event1   event2   event3
2016-04-27 10:25:15 11:05:45 13:00:09
2016-04-27 10:25:15 11:05:45 13:00:09
2016-04-27 10:25:15 11:05:45 13:00:09
2016-04-27 10:25:15 11:05:45 13:00:09
2016-04-27 10:25:15 11:05:45 13:00:09")