如何在R中将date和时间列移动6小时?

我有一个24小时制的date和时间栏。 现在我想把它转移6小时,使当天早上6点变成00:00,第二天早上6点结束。 在excel中,如果我们从date栏中减去0.25,则直接将date转换6个小时。 但类似的function似乎没有在R中工作。如何在R中实现这一点?

您应该提供有关您问题的更多信息,例如您正在使用的数据。

要用R复制,你可以使用lubridate包:

 library(lubridate) new_time <- time - hms("06:00:00") 

希望这可以帮助

一个使用base R的解决scheme。默认的算术运算添加秒,所以:

 now <- Sys.time() #gets the current time now "2016-04-22 09:52:21 CEST" now + 6*3600 "2016-04-22 15:52:21 CEST" 

有了你的数据,你可以尝试,围绕strptime

 df <- read.table(text=" DateTime 2/10/2016 19:18 2/10/2016 19:15 2/10/2016 19:12 2/10/2016 19:09 2/10/2016 19:06 2/10/2016 19:03", sep=";", h=T) df DateTime 1 2/10/2016 19:18 2 2/10/2016 19:15 3 2/10/2016 19:12 4 2/10/2016 19:09 5 2/10/2016 19:06 6 2/10/2016 19:03 df$NewTime <- strptime(as.character(df$DateTime), format="%d/%m/%Y %H:%M") 6*3600 df DateTime NewTime 1 2/10/2016 19:18 2016-10-03 01:18:00 2 2/10/2016 19:15 2016-10-03 01:15:00 3 2/10/2016 19:12 2016-10-03 01:12:00 4 2/10/2016 19:09 2016-10-03 01:09:00 5 2/10/2016 19:06 2016-10-03 01:06:00 6 2/10/2016 19:03 2016-10-03 01:03:00 

您可以在read.table移除stringsAsFactors = FALSEas.character步骤。 它解决你的问题吗?