如何在R中合并日期和时间?
中的R编程语言中的日期和时间的对象可以使用在R.的日期和时间的对象可以使用POSIX格式或合并在一起,在datetime对象的形式来表示。 POSIXlt 类存储日期和时间信息。
下面讨论了在 R 中组合日期和时间的各种方法。
方法一:使用M3包
R 语言中的 M3 包。 R 中的 combine.data.and.time() 方法可用于将日期和时间合并在一起以获得 POSIX 格式的日期时间对象。
Syntax: combine.date.and.time(date, time)
Parameter :
- date – Date can be specified either in the Date format or in the form of character string referred by “YYYY-MM-DD”.
- time – Time can be specified either in the form of a list consisting of hrs , mins and secs elements or in the form of character string referred by HH:MM:SS (with hours ranging from 00-23).
Returns :
Returns the date-time combined in the POSIX format.
默认情况下,返回的日期时间对象遵循 GMT 时区。如果日期或时间不是有效的日期或时间对象,则 NA 作为输出返回。
示例 1:
R
library("M3")
# declaring date
date_obj <- "2021-06-04"
# declaring time
time_obj <- "23:02:34"
# combining date and time into
# single object
combine.date.and.time(date = date_obj, time = time_obj)
R
library("M3")
# declaring date
date_obj <- "2021-06-04"
date_obj <- as.Date(date_obj)
# declaring time
time_obj <- "23:02:34"
# combining date and time into
# single object
combine.date.and.time(date = date_obj, time = time_obj)
R
library("M3")
# declaring date
date_obj <- "2021-06-04"
date_obj <- as.Date(date_obj)
# declaring time
time_obj <- list(hrs=22, mins=08, secs=35)
# combining date and time into
# single object
combine.date.and.time(date = date_obj, time = time_obj)
R
# declaring date
date_obj <- "2021-06-04"
date_obj <- as.Date(date_obj)
# declaring time
time_obj <- "22:08:35"
# specifying the format
format <- "%Y-%m-%d %H:%M:%S"
# combining date and time into single object
as.POSIXct(paste(date_obj, time_obj), format=format)
输出
[1] “2021-06-04 23:02:34 GMT”
也可以使用 R Date 对象指定日期来执行日期和时间组合操作。
示例 2:
电阻
library("M3")
# declaring date
date_obj <- "2021-06-04"
date_obj <- as.Date(date_obj)
# declaring time
time_obj <- "23:02:34"
# combining date and time into
# single object
combine.date.and.time(date = date_obj, time = time_obj)
输出
[1] “2021-06-04 23:02:34 GMT”
还可以使用由对象的小时、分钟和秒元素组成的 R 列表指定时间,以执行日期和时间组合操作。
示例 3:
电阻
library("M3")
# declaring date
date_obj <- "2021-06-04"
date_obj <- as.Date(date_obj)
# declaring time
time_obj <- list(hrs=22, mins=08, secs=35)
# combining date and time into
# single object
combine.date.and.time(date = date_obj, time = time_obj)
输出
[1] “2021-06-04 22:08:35 GMT”
方法二:as.POSIXct方法
可以使用 paste() 方法将日期和时间字符串一起转换为字符串。 POSIXct 方法由用于处理和操作属于“POSIXlt”和“POSIXct”的类的对象的函数组成,这些类表示日历日期和时间。指定的日期时间对象被转换为指定的字符串格式。
句法:
Syntax:
as.POSIXct(date-time, tz, format=”%Y-%m-%d %H:%M:%S”)
Parameter :
- date-time : The string date-time object to be converted to the specified format.
- tz : The time zone to convert the object into. Default is the UTC time zone.
Returns :
Returns the date-time combined in the POSIXct format.
例子:
电阻
# declaring date
date_obj <- "2021-06-04"
date_obj <- as.Date(date_obj)
# declaring time
time_obj <- "22:08:35"
# specifying the format
format <- "%Y-%m-%d %H:%M:%S"
# combining date and time into single object
as.POSIXct(paste(date_obj, time_obj), format=format)
输出
[1] “2021-06-04 22:08:35 IST”