How could I split the Time into hours with leading zero and minutes in below dataset. I tried sub with sprintf but didn't worked. Also tried str_sub and substr but couldn't get the desired output. Thanks.
dateorder <- structure(list(Date = c("30/04/2021", "30/04/2021", "30/04/2021", "30/04/2021", "30/04/2021", "30/04/2021", "30/04/2021", "30/04/2021", "30/04/2021", "30/04/2021"), Time = c(0L, 100L, 200L, 300L, 400L, 500L, 600L, 700L, 800L, 900L), Rainfall = c(0.4, 0.4, 0.6, 0.8, 0.8, 1, 0, 0, 0, 0), Date_Formatted = structure(c(18747, 18747, 18747, 18747, 18747, 18747, 18747, 18747, 18747, 18747 ), class = "Date"), Intensity = c(0.4, 0, 0.2, 0.2, 0, 0.2, -1, 0, 0, 0)), row.names = c(NA, 10L), class = "data.frame") sub("(\d{2})(\d{2})", "\1:\2",sprintf("d", dateorder$Time)) str_sub(dateorder$Time2,1,2) substr(dateorder$Time2,start = 1, stop = 2)
# Expected output
# Hr mm
# 00 00
# 01 00
# 02 00
CodePudding user response:
A base R option -
sprintf
adds a 0 as prefix if there is less than 4 digits inTime
column.strcapture
captures 1st 2 digits as hour (hh
) and last 2 as minutes (mm
).
strcapture('(\\d{2})(\\d{2})', sprintf('d', dateorder$Time),
proto = list(hh = character(), mm = character()))
# hh mm
#1 00 00
#2 01 00
#3 02 00
#4 03 00
#5 04 00
#6 05 00
#7 06 00
#8 07 00
#9 08 00
#10 09 00
Using tidyverse
-
library(tidyverse)
dateorder %>%
mutate(Time = str_pad(Time, 4, pad = '0')) %>%
extract(Time, c('hh', 'mm'), '(\\d{2})(\\d{2})')
CodePudding user response:
Without an example of your expected output it's not clear what you are really trying to achieve: it does not seem necessary to add leading zeros based on the data sample provided as all the Time
variables are in "HHMM" format.
Note all methods work (depending on what you want to achieve), Note also that the output is a character format which may or may not be what you require for further processing.
Going through the options you tried:
# `sub` method
# replace `time` with `Time` to get hours and minutes separated by a colon, leading zeros do not seem to be required, see above
sub("(\\d{2})(\\d{2})", "\\1:\\2", dateorder$Time)
#> [1] "10:00" "11:00" "12:00" "13:00" "14:00" "15:00" "16:00" "17:00" "18:00"
#> [10] "19:00"
# `stingr::str_sub` method
library(stringr)
# hours
str_sub(dateorder$Time, 1, 2)
#> [1] "10" "11" "12" "13" "14" "15" "16" "17" "18" "19"
#minutes
str_sub(dateorder$Time, 3, 4)
#> [1] "00" "00" "00" "00" "00" "00" "00" "00" "00" "00"
# substr method to get hours
substr(dateorder$Time, start = 1, stop = 2)
#> [1] "10" "11" "12" "13" "14" "15" "16" "17" "18" "19"
Created on 2021-12-19 by the reprex package (v2.0.1)
CodePudding user response:
First using strptime/strftime
, then strsplit
.
tm <- strptime(paste(Sys.Date(), dateorder$Time), '%F %H%M') |>
strftime('%H:%M')
# [1] "10:00" "11:00" "12:00" "13:00" "14:00" "15:00" "16:00" "17:00" "18:00" "19:00"
cbind(dateorder, tm=do.call(rbind, strsplit(tm, ':')))
# Date Time Rainfall Date_Formatted Intensity tm.1 tm.2
# 1 30/04/2021 1000 0.4 2021-04-30 0.4 10 00
# 2 30/04/2021 1100 0.4 2021-04-30 0.0 11 00
# 3 30/04/2021 1200 0.6 2021-04-30 0.2 12 00
# 4 30/04/2021 1300 0.8 2021-04-30 0.2 13 00
# 5 30/04/2021 1400 0.8 2021-04-30 0.0 14 00
# 6 30/04/2021 1500 1.0 2021-04-30 0.2 15 00
# 7 30/04/2021 1600 0.0 2021-04-30 -1.0 16 00
# 8 30/04/2021 1700 0.0 2021-04-30 0.0 17 00
# 9 30/04/2021 1800 0.0 2021-04-30 0.0 18 00
# 10 30/04/2021 1900 0.0 2021-04-30 0.0 19 00
Note: R version 4.1.2 (2021-11-01).