Last year, I used the code below to convert the character string to a datetime and it worked, but now I get unexpected results after running strptime
.
Data <- structure(list(time = c("12:00 AM", "1:00 AM", "2:00 AM",
"3:00 AM", "4:00 AM", "5:00 AM", "6:00 AM", "7:00 AM", "8:00 AM",
"9:00 AM", "10:00 AM", "11:00 AM")),
.Names = "time", class = "data.frame", row.names = c(NA, -12L))
Why does this give me the expected results from strptime:
strptime(Data$time[1:10], format="%l:%M %p")
# [1] "2013-03-01 00:00:00" "2013-03-01 01:00:00" "2013-03-01 02:00:00"
# [4] "2013-03-01 03:00:00" "2013-03-01 04:00:00" "2013-03-01 05:00:00"
# [7] "2013-03-01 06:00:00" "2013-03-01 07:00:00" "2013-03-01 08:00:00"
# [10] "2013-03-01 09:00:00"
But when I try to replace the existing data with the new data format, I get a warning and the unexpected results below:
Data$time[1:10] <- strptime(Data$time[1:10], format="%l:%M %p")
# Warning message:
# In Data$time[1:10] <- strptime(Data$time[1:10], format = "%l:%M %p") :
# number of items to replace is not a multiple of replacement length
Data
# time
# 1 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
# 2 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
# 3 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
# 4 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
# 5 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
# 6 113, 113, 113, 113, 113, 113, 113, 113, 113, 113
# 7 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
# 8 59, 59, 59, 59, 59, 59, 59, 59, 59, 59
# 9 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
# 10 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
# 11 10:00 AM
# 12 11:00 AM
When I run the code Data$time[1:10] <- strptime(Data$time[1:10], format="%l:%M %p")
in the question above and View(Data)
the resulting data in the standard RGui it looks as expected, however when I View(Data)
in RStudio the unexpected results above are displayed. In both RGui and RStudio the function class(Data$time)
, returns [1] "POSIXlt" "POSIXt"
and behaves as expected. This appears to be a problem with displaying the data in RStudio.