0

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!

xidelos
  • 37
  • 4
  • 1
    The error is not reproducible, this is working for me:df<-NULL df$TimeStamp<-c("09.02.2021 22:07:06.008", "10.02.2021 12:30:49.835", "08.02.2021 15:41:26.895", "13.02.2021 22:09:46.554", "19.01.2021 13:47:15.190", "08.02.2021 14:57:58.122", "08.02.2021 16:37:17.008", "06.02.2021 12:11:10.741", "07.02.2021 11:12:53.335", "05.02.2021 15:39:30.628" ) strptime(as.character(df$TimeStamp), format="%d.%m.%Y %H:%M:%OS") – Terru_theTerror Feb 18 '21 at 11:51
  • 1
    It is really strange, I think it has something to do with the number of observations, my df has 294 obs. So... I tried with lubridate and it worked: df$TimeStamp <- dmy_hms(df$TimeStamp) – xidelos Feb 18 '21 at 12:52

1 Answers1

0

I tried with lubridate package and it worked better:

df$TimeStamp <- dmy_hms(df$TimeStamp)

xidelos
  • 37
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 18 '22 at 07:13