Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

read xlsx in R with one column about Time. How to delete the date R added additionally?

I have one excel table and would like to use read_xlsx to convert it as an R dataframe.

Originally, in excel, the data looks like this:

dep_time
15:37:48
04:05:14

However, after I use the read_xlsx, the time column is something like below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

dep_time
1899-12-31 15:37:48
1899-12-31 04:05:14

I don’t want to have the date in my future figure plotting. How can I avoid this?

Thanks!

>Solution :

Here are two alternative ways to solve the problem.
They both use an auxiliary function to_times to extract the time part of the column value.

Base R

# sample data
x <- '1899-12-31 15:37:48
1899-12-31 04:05:14'

dep_time <- scan(text = x, what = character(), sep = "\n")
df1 <- data.frame(dep_time)

# code
to_times <- function(x) {
  y <- as.POSIXct(x)
  format(y, "%H:%M:%S")
}

df1$dep_time <- to_times(df1$dep_time)
df1
#>   dep_time
#> 1 15:37:48
#> 2 04:05:14

str(df1)
#> 'data.frame':    2 obs. of  1 variable:
#>  $ dep_time: chr  "15:37:48" "04:05:14"

Created on 2022-05-04 by the reprex package (v2.0.1)

Package chron

to_times <- function(x) {
  y <- sub("^[[:digit:]\\-]+ ([[:digit:]\\:]+)$", "\\1", x)
  chron::times(y)
}

df1$dep_time <- to_times(df1$dep_time)
df1
#>   dep_time
#> 1 15:37:48
#> 2 04:05:14

str(df1)
#> 'data.frame':    2 obs. of  1 variable:
#>  $ dep_time: 'times' num  15:37:48 04:05:14
#>   ..- attr(*, "format")= chr "h:m:s"

Created on 2022-05-04 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading