将数据框列转换为 R 中的日期时间
字符串类型的日期对象不提供组件的简单解析和操作。将日期对象转换为 POSIXct 或 POSIXlt 对象有助于将日期轻松转换为具有所需时区的所需格式。在本文中,我们将讨论如何在 R 编程语言中将数据帧列转换为字符串。
方法一:使用 as.POSIXct() 方法
日期字符串可以首先转换为 POSIXct 对象,然后可以轻松地对其执行基本算术。 POSIXct 对象简化了数学运算的过程,因为它们依赖秒作为时间管理的主要单位。日期将转换为标准时区 UTC。字符串类型的日期对象可以转换为 POSIXct 对象,在 R 中使用 as.POSIXct(date) 方法。
1 hour = 1 * 60 * 60 seconds
1 min = 1 * 60 seconds
POSIXct 中的“ct”表示日历时间,它存储自原点以来的秒数。它将字符串日期对象和格式说明符作为输入。 POSIXct 以秒为单位存储日期和时间,秒数从 1970 年 1 月 1 日开始。
as.POSIXct( date, format)
代码:
R
# declaring a data frame
data_frame = data.frame(col1 = letters[1:4],
col2 = c(5:8) ,
col3 = c("2021-05-05 01:04:34",
"2021-03-06 03:14:44",
"2021-03-11 07:22:48",
"2021-02-02 11:54:56"))
print ("Original dataframe")
print (data_frame)
sapply(data_frame, class)
# converting to datetime object
data_frame[['col3']] <- as.POSIXct(data_frame[['col3']],
format = "%Y-%m-%d %H:%M:%S")
print ("Modified dataframe")
print (data_frame)
sapply(data_frame, class)
R
# declaring a data frame
data_frame = data.frame(col1 = letters[1:4],
col2 = c(5:8) ,
col3 = c("2021-05-05 01:04:34",
"2021-03-06 03:14:44",
"2021-03-11 07:22:48",
"2021-02-02 11:54:56"))
print ("Original dataframe")
print (data_frame)
sapply(data_frame, class)
# converting to datetime object
data_frame[['col3']] <- strptime(data_frame[['col3']],
format = "%Y-%m-%d %H:%M:%S")
print ("Modified dataframe")
print (data_frame)
sapply(data_frame, class)
R
# declaring a data frame
data_frame = data.frame(col1 = letters[1:4],
col2 = c(5:8) ,
col3 = c("15/12/2021 01:04:34",
"06/10/2021 03:14:44",
"11/04/2021 07:22:48",
"28/01/1994 11:54:56"))
print ("Original dataframe")
print (data_frame)
sapply(data_frame, class)
# converting to datetime object
data_frame[['col3']] <- strptime(data_frame[['col3']],
format = "%d/%m/%Y %H:%M:%S")
print ("Modified dataframe")
print (data_frame)
sapply(data_frame, class)
输出:
[1] "Original dataframe"
col1 col2 col3
1 a 5 2021-05-05 01:04:34
2 b 6 2021-03-06 03:14:44
3 c 7 2021-03-11 07:22:48
4 d 8 2021-02-02 11:54:56
col1 col2 col3
"factor" "integer" "factor"
[1] "Modified dataframe"
col1 col2 col3
1 a 5 2021-05-05 01:04:34
2 b 6 2021-03-06 03:14:44
3 c 7 2021-03-11 07:22:48
4 d 8 2021-02-02 11:54:56
$col1
[1] "factor"
$col2
[1] "integer"
$col3
[1] "POSIXct" "POSIXt"
方法 2:使用strptime()方法
R 中的 strptime 方法用于将字符向量(各种格式)直接转换为 POSIXlt 格式。 strptime 比以前的方法快,因为 strptime 只处理字符输入。
Syntax: strptime(date, format, tz = “”)
Parameters:
- date – The date in character format
- format – The format specifier of the input date
- tz – time zone (optional)
代码:
电阻
# declaring a data frame
data_frame = data.frame(col1 = letters[1:4],
col2 = c(5:8) ,
col3 = c("2021-05-05 01:04:34",
"2021-03-06 03:14:44",
"2021-03-11 07:22:48",
"2021-02-02 11:54:56"))
print ("Original dataframe")
print (data_frame)
sapply(data_frame, class)
# converting to datetime object
data_frame[['col3']] <- strptime(data_frame[['col3']],
format = "%Y-%m-%d %H:%M:%S")
print ("Modified dataframe")
print (data_frame)
sapply(data_frame, class)
输出:
[1] "Original dataframe"
col1 col2 col3
1 a 5 2021-05-05 01:04:34
2 b 6 2021-03-06 03:14:44
3 c 7 2021-03-11 07:22:48
4 d 8 2021-02-02 11:54:56
col1 col2 col3
"factor" "integer" "factor"
[1] "Modified dataframe"
col1 col2 col3
1 a 5 2021-05-05 01:04:34
2 b 6 2021-03-06 03:14:44
3 c 7 2021-03-11 07:22:48
4 d 8 2021-02-02 11:54:56
$col1
[1] "factor"
$col2
[1] "integer"
$col3
[1] "POSIXlt" "POSIXt"
格式说明符指示解析字符日期对象的方式。它将日期时间对象转换为 YYYY-MM-DD HH:MM:SS 对象。
电阻
# declaring a data frame
data_frame = data.frame(col1 = letters[1:4],
col2 = c(5:8) ,
col3 = c("15/12/2021 01:04:34",
"06/10/2021 03:14:44",
"11/04/2021 07:22:48",
"28/01/1994 11:54:56"))
print ("Original dataframe")
print (data_frame)
sapply(data_frame, class)
# converting to datetime object
data_frame[['col3']] <- strptime(data_frame[['col3']],
format = "%d/%m/%Y %H:%M:%S")
print ("Modified dataframe")
print (data_frame)
sapply(data_frame, class)
输出:
[1] "Original dataframe"
col1 col2 col3
1 a 5 15/12/2021 01:04:34
2 b 6 06/10/2021 03:14:44
3 c 7 11/04/2021 07:22:48
4 d 8 28/01/1994 11:54:56
col1 col2 col3
"factor" "integer" "factor"
[1] "Modified dataframe"
col1 col2 col3
1 a 5 2021-12-15 01:04:34
2 b 6 2021-10-06 03:14:44
3 c 7 2021-04-11 07:22:48
4 d 8 1994-01-28 11:54:56
$col1
[1] "factor"
$col2
[1] "integer"
$col3
[1] "POSIXlt" "POSIXt"