What should I do to make 'time/minute/second' data into formulaic 6 digit form?

I got the data that consists exact time, minute, and second point. But I cannot use it directly because :
For example, '5559' in the data means '00 hour 55 min 59 sec',
'0' for '00 hour 00 min 00 sec',
while '314' for '00 hour 03 min 14 sec'.

Regarding data structure, there are several columns and this time data is one of them.
And this is integer form.

Since I have to deal with the data at once, a data transformation into integrated form is required.
And I am about to use 6 digit form of HHMMSS, and I think the originally it was constructed like '005559', '000000', '000314', but lost 0s in the process of importing files into R, because typically 0 can't come first.
So my question is this : Which function or libraries do I have to use to transform these data into 6-digit code?

I would try to read in the data in a way that the leading zeros are not lost. It would then be simple to parse the six digits into hours, minutes and seconds. For example, I made a csv file with this content:

Time,Value
000000,23
000314,1.45
005559,812
121323,6

If I read it in with the default settings of the read.csv function, the leading zeros are dropped. If I set the class of the Time column to character, the leading zeros are preserved. Can you do something similar with your data?

DF <- read.csv("~/R/Play/Dummy.csv",)
DF
#>     Time  Value
#> 1      0  23.00
#> 2    314   1.45
#> 3   5559 812.00
#> 4 121323   6.00
DF <- read.csv("~/R/Play/Dummy.csv",colClasses = c("character","numeric"))
DF
#>     Time  Value
#> 1 000000  23.00
#> 2 000314   1.45
#> 3 005559 812.00
#> 4 121323   6.00

Created on 2022-09-07 with reprex v2.0.2

If you can't re-read the data, you can use stringr:str_pad()

library(dplyr)
library(stringr)

sample_df <- data.frame(
    time_col = c(5559, 0, 314)
)

sample_df %>%
    mutate(time_col = str_pad(time_col, width = 6, side = "left", pad = 0))
#>   time_col
#> 1   005559
#> 2   000000
#> 3   000314

Created on 2022-09-07 with reprex v2.0.2

Then you can parse it into a proper HMS format if needed

library(dplyr)
library(stringr)
library(lubridate)
library(hms)

sample_df <- data.frame(
    time_col = c(5559, 0, 314)
)

sample_df %>%
    mutate(time_col = str_pad(time_col, width = 6, side = "left", pad = 0),
           time_col = as_hms(parse_date_time(time_col, orders = "%H%M%S")) )
#>   time_col
#> 1 00:55:59
#> 2 00:00:00
#> 3 00:03:14

Created on 2022-09-07 with reprex v2.0.2

1 Like

It worked. Thank you so much...!

I faced on the different problem. At first I asked you guys 'how to read files with preserving leading zeros', but while I was doing further I had to figure out 'how to convert the figures into the form I wanted', for some reason.
If I saw the answer quicker I wouldn't have taken a detour...thank you. I'll do like that later.
But what I wonder is, that 'colclasses' works in 'fread'? Because I usually use fread instead of read.csv.

yes, fread has colClasses argument

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.