0

I am working on twitter data by useing R and I have column for date and time like this "10/19/2018 23:00" , "10/19/2018 23:01", "10/19/2018 23:02" ,"10/19/2018 19:45". I want to change the format to be without date and also the time be without minute for example any time on 23 hour I need it to be on 23 only without minute.
this is an example of the column:

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

and I need it to be like this 15:00 23:00 16:00

hope you understand me can you help me to do it and thank you.

Fatima
  • 497
  • 5
  • 21

2 Answers2

0

We can use sub

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

Or convert to a DateTime class and then extract the components with format

format(as.POSIXct(df1$created, format = "%m/%d/%Y %H:%M"), "%H:00")
#[1] "23:00" "23:00" "23:00" "23:00"

data

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)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • yes thank you, first one is worked but it's removed the date only I need to remove the minute also. I need the hour only – Fatima Nov 28 '18 at 16:57
  • If you check the output, you can see that it is working fine – akrun Nov 28 '18 at 17:09
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"
Andre Elrico
  • 10,956
  • 6
  • 50
  • 69