0

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.

Kev
  • 118,037
  • 53
  • 300
  • 385
cwhit25
  • 11
  • 4
  • Is it normal that you have negative times? – agstudy Mar 01 '13 at 18:17
  • 2
    Also, shouldn't your "format" argument match the structure of the input? – A5C1D2H2I1M1N2O1R2T1 Mar 01 '13 at 18:18
  • And *what* is the format of your input? – Thilo Mar 01 '13 at 18:19
  • They format correctly in line 1 to the correct times... The format is originally numeric. – cwhit25 Mar 01 '13 at 18:38
  • the times are all preceded by the date 1900-01-01 but R uses 1970-01-01 as 1 so these are negative – cwhit25 Mar 01 '13 at 18:53
  • for more on the way that I am using the format function see http://www.statmethods.net/input/dates.html @AnandaMahto – cwhit25 Mar 01 '13 at 19:02
  • @cwhit25 just looking at your first line - `1244174400 -2209143600` - what are your expected results? :) – Anthony Damico Mar 01 '13 at 19:04
  • @AnthonyDamico The expected results for DateTime for the first observation is 2009-06-05 00:00:00. Oh and a side note the times have 1899-12-30 originally appended. – cwhit25 Mar 01 '13 at 19:16
  • 1
    Ok, and how do you go from 1244174400 -2209143600 to 2009-06-05 00:00:00? – A5C1D2H2I1M1N2O1R2T1 Mar 01 '13 at 19:17
  • @AnandaMahto does the information about the expected results above help? – cwhit25 Mar 01 '13 at 19:28
  • @cwhit25 could you explain, step by step, exactly how the input number is _supposed_ to be converted to `2009-06-05 00:00:00`? don't worry about code, just explain it in words :) – Anthony Damico Mar 01 '13 at 19:37
  • @AnthonyDamico so line 1: removes the 1899-12-30 so that Time is just the time; line 2: concatenates the two strings from DATE and TIME into one field; line 3: converts this to datetime format. strptime seems to be working correctly as shown in the edit above. the problem seems to happen when assigning it to the variable. – cwhit25 Mar 01 '13 at 19:45
  • @cwhit25 but please don't tell us what your broken code is supposed to do. just tell us what tasks a human would perform to convert your given input to your desired output. :) – Anthony Damico Mar 01 '13 at 19:54
  • I updated the question to reflect more of what I am really trying to ask. Please focus on the update at the bottom of the question. – cwhit25 Mar 01 '13 at 20:38
  • Your update does not help without sample data. There is no way for anyone to [reproduce your issue](http://stackoverflow.com/q/5963269/271616), which makes it incredibly hard to help you. – Joshua Ulrich Mar 01 '13 at 21:18
  • @JoshuaUlrich I added the data for the updated question – cwhit25 Mar 01 '13 at 21:34
  • Upon further inspection, this appears to be a bug with the display of the data in the RStudio GUI that I am using... – cwhit25 Mar 01 '13 at 22:04
  • WOW. This question has changed *a lot* since its first edit. @cwhit25, with that last comment, are you trying to say your problem is solved? – A5C1D2H2I1M1N2O1R2T1 Mar 02 '13 at 10:08

1 Answers1

1

I don't know how this would have worked in the past, unless it was supported in an earlier version of R...

strptime returns a POSIXlt object, which is a list with several components (see ?POSIXlt for a description of the elements). The problem arises because you try to replace a portion of a character vector with a POSIXlt object. This converts the entire column into a list, where the first 10 elements are the components of the POSIXlt object, and the last two are the original elements from the character vector.

Everything will work fine if you replace the entire column with the result from strptime or make a new column with the result from strptime:

Data$time2 <- strptime(Data$time, format="%l:%M %p")
Data$time  <- strptime(Data$time, format="%l:%M %p")
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • The code in the question above does work and I was only shortening it to the first few rows because I have a 130,000+ observations and didn't think that would be appropriate to include in the question. – cwhit25 Mar 07 '13 at 16:49
  • @cwhit25: you don't have to include _all_ your data to make a reproducible example. You could find a subset of the data that illustrates the issue. – Joshua Ulrich Mar 07 '13 at 16:58
  • The point is the data displays in RStudio in the wrong format whether using a portion of or the ENTIRE column even though it responds, behaves, and prints correctly in the console. It only looks unusual when using View() in RStudio. In RGui, View() displays the correct datetime. – cwhit25 Mar 07 '13 at 17:05
  • @cwhit25: then file a bug report with RStudio... – Joshua Ulrich Mar 07 '13 at 17:06
  • Right... I just feel that the question and answer are now kind of misleading. I tried to answer it but it got deleted. Sorry this is the first time I have used SO and obviously have a lot to learn about how all this works. – cwhit25 Mar 07 '13 at 18:21