Search code examples
rdatetimeposixctstrptimeposixlt

R Error converting column from character to datetime


I want to use strptime to convert a column from character to date-time format in an R data.frame.

This is how the data from the column look like (in total I have 294 obs.):

> df$TimeStamp
  [1] "09.02.2021 22:07:06.008" "10.02.2021 12:30:49.835"
  [3] "08.02.2021 15:41:26.895" "13.02.2021 22:09:46.554"
  [5] "19.01.2021 13:47:15.190" "08.02.2021 14:57:58.122"
  [7] "08.02.2021 16:37:17.008" "06.02.2021 12:11:10.741"
  [9] "07.02.2021 11:12:53.335" "05.02.2021 15:39:30.628"
... 
  [293] "09.02.2021 12:07:56.473" "09.02.2021 19:49:13.270"

I tried to convert the column to a date-time format with:

df$TimeStamp <- strptime(as.character(df$TimeStamp), format="%d.%m.%Y %H:%M:%OS")

However, I am getting the following error:

Error in set(x, j = name, value = value) : Supplied 11 items to be assigned to 294 items of column 'TimeStamp'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.

I have no idea what could be wrong. I hope someone has an idea for what might cause the problem, so I can use the date-time data to order the data frame chronologically.

Thanks!


Solution

  • I tried with lubridate package and it worked better:

    df$TimeStamp <- dmy_hms(df$TimeStamp)