更改R中的时间和日期格式

时间:2018-11-28 16:46:53

标签: r sentiment-analysis

我正在使用R处理Twitter数据,并且具有日期和时间列,例如“ 10/19/2018 23:00”,“ 10/19/2018 23:01”,“ 10/19/2018” 23:02“,” 10/19/2018 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

希望你了解我 你能帮我做吗,谢谢你。

2 个答案:

答案 0 :(得分:0)

我们可以使用sub

sub('.*\\s(.{2}).*', '\\1:00', df1$created)
#[1] "23:00" "23:00" "23:00" "23:00"

或转换为DateTime类,然后使用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)

答案 1 :(得分:0)

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"