0

After I imported the text file into R, R omitted "0" in the time column.

For example:

Before import time | After import time
077250             | 77250
000002             |     2

Thus, unable to convert to the correct time format. (from 77250 to 07:25:50)

How can i convert the integer time to the correct time format?

I have tried:

chron (time, "%H:%M:%S")
strptime(time, "%H:%M:%S")
time <- as.hms(time)
Cettt
  • 11,460
  • 7
  • 35
  • 58
momoni
  • 33
  • 11
  • 4
    How did you read the file? That's a string, not an integer. You should modify your import code to treat that field as a string – Panagiotis Kanavos May 23 '19 at 08:49
  • I uses , read.delim method to import the text file... how can i import code to treat the field as a string? – momoni May 23 '19 at 08:53
  • @momoni post your code in the question itself. – Panagiotis Kanavos May 23 '19 at 08:54
  • @momoni in any case, `read.delim` is an alias for `read.table`. You can use the `as.is` parameter to prevent the conversion of text fields to numbers or factors. You can pass the column classes through `colClasses` – Panagiotis Kanavos May 23 '19 at 08:57
  • Related, possible duplicate of https://stackoverflow.com/q/14854485/680068 – zx8754 May 23 '19 at 08:58
  • @Ronak Shah yes! sorry about that. Im intending to convert 072550 to 07:25:50 (HH:MM:SS) format – momoni May 23 '19 at 09:01
  • @momoni Check the second duplicate. You could load the string as a `Date` directly. The reason `read.csv(con, colClasses=c('numeric','myDate'), header=FALSE)` works even though `read.csv` isn't supposted to have a `colClasses` parameter is that any extra parameters are passed to the underlying `read.table` call directly – Panagiotis Kanavos May 23 '19 at 09:02

3 Answers3

3

You can use str_pad from the stringr package to restore the zeroes:

library(stringr)
time_old <- "2"
time_new <- str_pad(time_old, width = 6, side = "left", pad = 0)

Then, you should be able to use the chron function:

chron::chron(times = time_new, format = list(times = "hms"),
             out.format = "h:m:s")
[1] 00:00:02
Cettt
  • 11,460
  • 7
  • 35
  • 58
  • Hi @cettt , thanks for your reply !! As, i have a few dataset has to go through this process and time colomn is actually in a dataframe X1. how can i change it for that X[i]$time column which it goes in a loop. so that it can be used for dataset X2 X3 X4.. – momoni May 27 '19 at 10:08
3

We can use sprintf and strptime/as.POSIXct

If you have read them as numeric use %d in sprintf or use %s if they are characters.

x <- c(072550, 2)
format(strptime(sprintf("%06d", x), "%H%M%S"), "%T")
#[1] "07:25:50" "00:00:02"

x <- c("072550", "2")
format(strptime(sprintf("%06s", x), "%H%M%S"), "%T")
#[1] "07:25:50" "00:00:02"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

This possibly duplicate question shows how to read the data in the format you want directly, by specifying your own formatting function through colClasses :

setAs("character","myDate", function(from) as.Date(from, format="%Y%m%d") )
setAs("character","myTime", function(from) chron(times = from, format = "hms", out.format = "h:m:s"))

tmp <- c("1\t20080815\t072550", "2\t20100523\t000002")
con <- textConnection(tmp)

tmp2 <- read.delim(con, colClasses=c('numeric','myDate','myTime'), header=FALSE)

tmp2 contains :

  V1         V2       V3
1  1 2008-08-15 07:25:50
2  2 2010-05-23 00:00:02

read.delim is a shortcut for read.table that sets a few defaults and passes any extra parameters like colClasses directly to read.table

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236