更改R中时间和日期的格式
change the format of time and date in R
我正在使用 R 处理 Twitter 数据,我有这样的日期和时间列“10/19/2018 23:00”,“10/19/2018 23:01”, “2018 年 10 月 19 日 23:02”,“2018 年 10 月 19 日 19:45”。
我想将格式更改为没有日期,时间也没有分钟,例如 23 小时的任何时间我需要它只在 23 没有分钟。
这是列的示例:
created
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 15:32
10/19/2018 15:32
10/19/2018 15:32
10/19/2018 15:32
10/19/2018 15:32
10/19/2018 15:32
10/19/2018 15:32
10/19/2018 15:32
我需要它像这样
15:00
23:00
16:00
希望你能理解我
你能帮我做一下吗,谢谢。
我们可以使用sub
sub('.*\s(.{2}).*', '\1:00', df1$created)
#[1] "23:00" "23:00" "23:00" "23:00"
或转换为 DateTime
class 然后用 format
提取组件
format(as.POSIXct(df1$created, format = "%m/%d/%Y %H:%M"), "%H:00")
#[1] "23:00" "23:00" "23:00" "23:00"
数据
df1 <- data.frame(created = c('10/19/2018 23:29',
'10/19/2018 23:29', '10/19/2018 23:29',
'10/19/2018 23:29'), stringsAsFactors = FALSE)
x<-
c("10/19/2018 23:29", "10/19/2018 23:29", "10/19/2018 23:29", "10/19/2018 23:29")
paste0(unlist(regmatches(x, gregexpr("(?<=\s)\d{2}(?=:\d{2})", x, perl = T))), ":00")
#[1] "23:00" "23:00" "23:00" "23:00"
我正在使用 R 处理 Twitter 数据,我有这样的日期和时间列“10/19/2018 23:00”,“10/19/2018 23:01”, “2018 年 10 月 19 日 23:02”,“2018 年 10 月 19 日 19:45”。
我想将格式更改为没有日期,时间也没有分钟,例如 23 小时的任何时间我需要它只在 23 没有分钟。
这是列的示例:
created
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:29
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 23:28
10/19/2018 15:32
10/19/2018 15:32
10/19/2018 15:32
10/19/2018 15:32
10/19/2018 15:32
10/19/2018 15:32
10/19/2018 15:32
10/19/2018 15:32
我需要它像这样 15:00 23:00 16:00
希望你能理解我 你能帮我做一下吗,谢谢。
我们可以使用sub
sub('.*\s(.{2}).*', '\1:00', df1$created)
#[1] "23:00" "23:00" "23:00" "23:00"
或转换为 DateTime
class 然后用 format
format(as.POSIXct(df1$created, format = "%m/%d/%Y %H:%M"), "%H:00")
#[1] "23:00" "23:00" "23:00" "23:00"
数据
df1 <- data.frame(created = c('10/19/2018 23:29',
'10/19/2018 23:29', '10/19/2018 23:29',
'10/19/2018 23:29'), stringsAsFactors = FALSE)
x<-
c("10/19/2018 23:29", "10/19/2018 23:29", "10/19/2018 23:29", "10/19/2018 23:29")
paste0(unlist(regmatches(x, gregexpr("(?<=\s)\d{2}(?=:\d{2})", x, perl = T))), ":00")
#[1] "23:00" "23:00" "23:00" "23:00"