Hi I have a task of sorting daily open hours into a usable format more similar to google's opening hours, e.g:
Monday 06:00 - 18:00
Tuesday 08:00 - 18:00
...
Sunday Closed
There is a column opening hours 100K+ but a sample shown below of the irregularly formats. I thought I could use regular expressions to use the group matches and assign where found but I can't find a way of not using the group match when they are NA.
Times <- data.frame(opening_hours = c("Su 09:00-17:00,M-S 08:00-20:00",
"Su-S 06:00-23:59",
"Su-S 08:00-22:00",
"Su 08:00-22:00,M-F 08:30-22:00,S 08:00-22:00",
"Su-S 00:00-23:59",
"M-F 08:00-22:00",
"M-W 08:00-22:00, Th-F 08:00-22:00"))
df <- as.data.frame(str_match(Times$opening_hours,"(?i)Su-S (\\d\\d:\\d\\d)-(\\d\\d:\\d\\d)"))
Times$Monday <- paste(df$V2, df$V3,sep = "-")
Times$Tuesday <- paste(df$V2, df$V3,sep = "-")
Times$Wednesday <- paste(df$V2, df$V3,sep = "-")
Times$Thursday <- paste(df$V2, df$V3,sep = "-")
Times$Friday <- paste(df$V2, df$V3,sep = "-")
Times$Satuarday <- paste(df$V2, df$V3,sep = "-")
Times$Sunday <- paste(df$V2, df$V3,sep = "-")
df <- as.data.frame(str_match(Times$opening_hours,"(?i)Su (\\d\\d:\\d\\d)-(\\d\\d:\\d\\d)"))
Times$Sunday <- paste(df$V2, df$V3,sep = "-")
Continued for each range format I can find in the data which isn't elegant but if I could conditionally check for V2 and V3 being NA because no date was found and not paste the values in it would work but if there is a better approach I am all ears.
Many thanks, Leo.