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

How to find MAX of a time type column from an imported csv table in R?

Hi I am completely new to R-programming, for a project need to find the max of a time type column from an imported csv file.

preview of the column: of the time type format %H:%M:%S

enter image description here

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

table name : X202302_divvy_tripdata

Code used to find the MAX:

library(chron)
ride_length_chron <- chron(times. = combined_table$ride_length)
max_ride_length <- max(ride_length_chron)
max_ride_length_format <- format(max_ride_length, format. = "%H:%M:%S")
max_ride_length_format

In this code I am getting an error as the output is showing as : "NA"

>Solution :

I’ve never used the chron package, but this seems to work if you have a character variable after you’ve imported your data:

library(chron)

# assertion to check data type since this is not clear in the question
stopifnot(class(combined_table$ride_length) == "character")

ride_times <- times(combined_table$ride_length)
max(ride_times)

If you have missing values in the ride_length column, you can either change the raw data, or exclude the missing values when calculating the maximum:

max(ride_times, na.rm = TRUE)

The times function within the chron package returns at times class which is non-standard. After calculating your maximum time, you could coerce the value to a character if easier to work with.

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