如何合并一列中的日期和另一列中的时间?

我从Excel导入了一些数据,这些数据具有“日期”和“时间”的单独列。当我导入“时间”列时,它为每个单个时间值返回1899-12-31 19:00:00,并带有日期1899-12-31

我想创建一个新列,该列将合并“日期”列中的日期和“时间”列中的时间,以便进行一些计算。

# A tibble: 207 x 2
   DoS                 ToS                
   <dttm>              <dttm>             
 1 2018-01-27 00:00:00 1899-12-31 19:00:00
 2 2018-02-07 00:00:00 1899-12-31 15:45:00
 3 2018-02-13 00:00:00 1899-12-31 23:00:00
 4 2018-02-15 00:00:00 1899-12-31 13:45:00
 5 2018-02-16 00:00:00 1899-12-31 10:00:00
 6 2018-02-19 00:00:00 1899-12-31 15:00:00
 7 2018-02-20 00:00:00 1899-12-31 15:05:00
 8 2018-02-21 00:00:00 1899-12-31 15:00:00

> dput(head(sample,10))
structure(list(DoS = structure(c(1517011200,1517961600,1518480000,1518652800,1518739200,1518998400,1519084800,1519171200,1519257600,1519862400),class = c("POSIXct","POSIXt"),tzone = "UTC"),ToS = structure(c(-2209006800,-2209018500,-2208992400,-2209025700,-2209039200,-2209021200,-2209020900,-2209033800,-2209005000),tzone = "UTC")),class = c("tbl_df","tbl","data.frame"),row.names = c(NA,-10L))

有什么方法可以提取时间值并将其粘贴到“日期”列中?

lz407854504 回答:如何合并一列中的日期和另一列中的时间?

使用基数R,我们可以从DoS中提取日期,并从ToS中提取时间,并将它们组合在一起。

transform(sample,Datetime = as.POSIXct(paste(as.Date(DoS),format(ToS,"%T"))))

#          DoS                 ToS            Datetime
#1  2018-01-27 1899-12-31 19:00:00 2018-01-27 19:00:00
#2  2018-02-07 1899-12-31 15:45:00 2018-02-07 15:45:00
#3  2018-02-13 1899-12-31 23:00:00 2018-02-13 23:00:00
#4  2018-02-15 1899-12-31 13:45:00 2018-02-15 13:45:00
#5  2018-02-16 1899-12-31 10:00:00 2018-02-16 10:00:00
#6  2018-02-19 1899-12-31 15:00:00 2018-02-19 15:00:00
#7  2018-02-20 1899-12-31 15:05:00 2018-02-20 15:05:00
#8  2018-02-21 1899-12-31 15:00:00 2018-02-21 15:00:00
#9  2018-02-22 1899-12-31 11:30:00 2018-02-22 11:30:00
#10 2018-03-01 1899-12-31 19:30:00 2018-03-01 19:30:00
本文链接:https://www.f2er.com/3170093.html

大家都在问