1

I have a data frame in R where, the numeric data represents a series of time in 24 hour format. The catch here is the data is not entered correctly. The data is inconsistent in terms of the four digits to be entered.

A sample of the data can be found here:

A sample of few rows

As it is visible in the data here, 1) is in correct and desired format.

The second entry is meant to be 0100, but the zero at the start have been lost, or entered incorrectly.

Similarly, for the rest of the data 3) 9 in place of 0009 4) 110 in place of 0110 5) 10 in place of 0010 6) 0 in place of 0000 7) 209 in place of 0209 8) 330 in place of 0330

This is a part of a larger data frame with loads of data discrepancies, there are a total of 1652 data entries and all of them needs to be pre-processed before we start analysing in R.

Is there any function/library/code available to get the required output.

Just for another sample, I'm attaching another picture which will help you to validate the code:

Validation Data

  1. 2330
  2. 0115
  3. 0034
  4. 0130
  5. 0020
  6. 0000
  7. 0257
  8. 0330

Cheers

driver
  • 273
  • 1
  • 13
  • 1
    `stringr::str_pad(your_data, width = 4, side = "left", pad = "0")` – Gregor Thomas Apr 03 '23 at 13:01
  • 2
    If your file has all the leading 0s and they were lost when you imported it, the issue is a conversion to numeric that never should have happened. Whatever function you used to import the data probably has an argument named `colClasses` or similar that can let you specify that certain columns like this one should be read in as `character` not `numeric`. – Gregor Thomas Apr 03 '23 at 13:04
  • @GregorThomas your function works but turns out the data type changes from numeric to character. i would like to have a numeric data type and at the same time preserve the proceeding zeros – driver Apr 03 '23 at 15:57
  • 1
    Numerics don't have leading 0s. You can pick either leading 0s or numeric data type, you can't have both. – Gregor Thomas Apr 03 '23 at 16:24
  • 1
    You may also be interested in the `hms` class in the [`hms` package](https://hms.tidyverse.org/reference/hms.html) – Gregor Thomas Apr 03 '23 at 16:25

0 Answers0