[英]How to split a date and time column into separate day, month, hour, minute, second, day of week columns in r?
I'm trying to split a date and time column into separate day, month, hour, minute, second, day of week columns.我正在尝试将日期和时间列拆分为单独的日、月、小时、分钟、秒、星期几列。 I'm using the lubridate and mutuate function but when I try to use the below code I get this error: Warning message: All formats failed to parse.我正在使用 lubridate 和 mutuate function 但是当我尝试使用以下代码时,我收到此错误:警告消息:所有格式都无法解析。 No formats found.未找到格式。
My new columns are created but they all contain NA's - wondering if someone can help?我的新列已创建,但它们都包含 NA - 想知道是否有人可以提供帮助?
My Column looks like this:我的专栏是这样的:
tpep_pickup_datetime
01/07/2019 00:51:15
01/07/2019 00:46:30
01/07/2019 00:25:35
and my code is this:我的代码是这样的:
taxidata3 <- taxidata2 %>%
mutate(tpep_pickup_datetime = mdy_hms(tpep_pickup_datetime),
day = day(tpep_pickup_datetime),
month = month(tpep_pickup_datetime),
year = year(tpep_pickup_datetime),
dayofweek = wday(tpep_pickup_datetime),
hour = hour(tpep_pickup_datetime),
minute = minute(tpep_pickup_datetime),
second = second(tpep_pickup_datetime))
Based on the comments from the OP, the date format is day/month/... instead of month/day/... Here, we need dmy_hms
.根据 OP 的评论,日期格式是 day/month/... 而不是 month/day/... 这里,我们需要dmy_hms
。 So, each letter signifies the order of occurrence因此,每个字母表示出现的顺序
library(lubridate)
library(dplyr)
taxidata3 <- taxidata2 %>%
mutate(tpep_pickup_datetime = dmy_hms(tpep_pickup_datetime),
day = day(tpep_pickup_datetime),
month = month(tpep_pickup_datetime),
year = year(tpep_pickup_datetime),
dayofweek = wday(tpep_pickup_datetime),
hour = hour(tpep_pickup_datetime),
minute = minute(tpep_pickup_datetime),
second = second(tpep_pickup_datetime))
This is a stringr
solution using regex to match the date components:这是一个使用正则表达式匹配日期组件的stringr
解决方案:
Data:数据:
df <- data.frame(
tpep_pickup_datetime = c("01/07/2019 00:51:15", "01/07/2019 00:46:30", "01/07/2019 00:25:35")
)
Solution:解决方案:
library(stringr)
df$day <- str_extract(df$tpep_pickup_datetime, "^\\d{2}")
df$month <- str_extract(df$tpep_pickup_datetime, "(?<=/)\\d{2}")
df$year <- str_extract(df$tpep_pickup_datetime, "\\d{4}")
df$hour <- str_extract(df$tpep_pickup_datetime, "(?<= )\\d{2}(?=:)")
df$minute <- str_extract(df$tpep_pickup_datetime, "(?<=:)\\d{2}(?=:)")
df$second <- str_extract(df$tpep_pickup_datetime, "(?<=:)\\d{2}$")
Result:结果:
df
tpep_pickup_datetime day month year hour minute second
1 01/07/2019 00:51:15 01 07 2019 00 51 15
2 01/07/2019 00:46:30 01 07 2019 00 46 30
3 01/07/2019 00:25:35 01 07 2019 00 25 35
Here is an other alternative using the separate
function.这是使用separate
的 function 的另一种选择。 The code and the output is as follows:-代码和output如下:-
library(tidyverse)
df <- data.frame(
tpep_pickup_datetime = c("01/07/2019 00:51:15", "01/07/2019 00:46:30",
"01/07/2019 00:25:35"))
df %>%
separate(tpep_pickup_datetime, c("Day", "Month", "Year_time"),
sep = "/", remove = FALSE) %>%
separate(Year_time, c("Year", "Time"),
sep = " ", remove = TRUE) %>%
separate(Time, c("Hour", "Minute", "Second"),
sep = ":", remove = TRUE)
# tpep_pickup_datetime Day Month Year Hour Minute Second
#1 01/07/2019 00:51:15 01 07 2019 00 51 15
#2 01/07/2019 00:46:30 01 07 2019 00 46 30
#3 01/07/2019 00:25:35 01 07 2019 00 25 35
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.