Hi,
It's not entirely clear what you want to do, but here is some code that addresses step 1 and 2, though step 2 assumes all you need is a list of dates and hours (no other columns)
library(stringr)
library(dplyr)
library(tidyr)
myData = data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
timeStamp = c("6/4/2012 2:00", "6/4/2012 3:00","6/4/2012 12:00","6/4/2012 13:00",
"6/4/2012 16:00","6/4/2012 20:00","6/4/2012 22:00",
"6/4/2012 23:00","6/5/2012 3:00",
"6/5/2012 15:00","6/5/2012 20:00","6/6/2012 2:00")
)
#Split into two columns
myData = myData %>% separate(timeStamp, into = c("date", "time"), sep = " ")
#Extract the hour
myData = myData %>% mutate(hour = as.integer(str_extract(time, "^\\d+")))
myData
#> date time hour
#> 1 6/4/2012 2:00 2
#> 2 6/4/2012 3:00 3
#> 3 6/4/2012 12:00 12
#> 4 6/4/2012 13:00 13
#> 5 6/4/2012 16:00 16
#> 6 6/4/2012 20:00 20
#> 7 6/4/2012 22:00 22
#> 8 6/4/2012 23:00 23
#> 9 6/5/2012 3:00 3
#> 10 6/5/2012 15:00 15
#> 11 6/5/2012 20:00 20
#> 12 6/6/2012 2:00 2
#Create new data frame with 24 hours for each date
expandedData = data.frame(date = rep(unique(myData$date), each = 24),
hour = rep(0:23, length(unique(myData$date))))
head(expandedData)
#> date hour
#> 1 6/4/2012 0
#> 2 6/4/2012 1
#> 3 6/4/2012 2
#> 4 6/4/2012 3
#> 5 6/4/2012 4
#> 6 6/4/2012 5
Created on 2020-08-07 by the reprex package (v0.3.0)
In the second part, I used every date, and generate 24 hours for it (0-23). This is a new data frame, and not really dependent on the previous one (apart from knowing the dates). If you have other columns in the original data frame, they won't be in this one.
Hope this helps,
PJ