“Handling Format Inconsistencies in Time Variable When Importing Data in R”

I have a dataset in R where a time variable has been imported as text. This is because, not specifying it to be imported as text results in many observations being converted to NAs. However, I’ve discovered that the time variable has inconsistent formatting. Some rows have numeric values (for example, 0.24962962962962965), while others have the HH:MM:SS format (for example, 07:19:52). My goal is to convert this variable into a consistent HH:MM:SS time format in R.

How can I address this situation and convert the time variable to a consistent HH:MM:SS format for the entire dataset?

I’ve tried some approaches using mathematical operations and conversion functions, but I’m unsure how to handle both numeric values and time formats in a single column.

I’ve attached simplified data for this case:

datos_texto <- c("0.24962962962962965", "07:19:52", "0.123456789", "10:45:30", "0.567891234")

I would greatly appreciate any advice or code examples that could help me solve this issue and obtain a time variable in the desired format.

Thank you in advance for your assistance!

  • In what units are the decimals?

    – 

  • @Phil, when I import the data from Excel to R, in Excel, the time appears in the regular time format. However, when I import it into R as text, it gets converted into a number with several decimal places

    – 

Leave a Comment