0

I have a database where I have a list of ocurrences with three sets of dates and times (total of six columns). I want to convert this to POSIX to manipulate later. The problem is that after creating three new columns to paste the date and time to a single string, when I try to convert to POSIX, only the first one is sucessful, the last two contain only the date.

At first I thought that maybe some entries didn't have a time, but I checked and there are rows where there are both things and only date is returned.

The date is in an european format first, the columns with dates and times are also factors when I read the data. I'm using as.Date(data$DateAlert, format = '%d/%m/%Y') to convert to the new format and later I'm using paste(data$DateAlert, data$HourAlert), creating a string in the POSIX format like 2001-07-29 14:53:10. I have confirmed that the columns containing these strings are in the type character, so I guess POSIXct wouldn't have much trouble with it.

Before converting to POSIX, I get something like this:

$ Alert      : chr  "2001-07-29 15:32:00" "2001-07-02 12:59:00" "2001-07-31 23:37:00" "2001-08-01 17:02:00"

$ Complete    : chr  "2001-07-29 17:20:00" "2001-07-03 02:39:00" "2001-08-01 02:53:00" "2001-08-01 17:24:00"

$ Inter       : chr  "2001-07-29 14:53:10" "2001-07-02 12:26:32" "2001-07-31 22:37:57" "2001-08-01 16:19:25"

After converting to POSIX, using as.POSIXct() I get something like this:

$ Alert      : POSIXct, format: "2001-07-29 15:32:00" "2001-07-02 12:59:00" "2001-07-31 23:37:00" "2001-08-01 17:02:00" ...

$ Complete    : POSIXct, format: "2001-07-29" "2001-07-03" "2001-08-01" "2001-08-01" ...

$ Inter       : POSIXct, format: "2001-07-29" "2001-07-02" "2001-07-31" "2001-08-01" ...

Any idea of what am I doing wrong? I haven't found any similar thread with this problem

Hugo Torres
  • 308
  • 4
  • 13
  • 1
    Hard to answer without the actually data, since the problem is likely some detail about the object you are passing to as.POSIXct(). Could you post a dput of the object you are using in that function. – trosendal Feb 16 '18 at 15:25
  • @trosendal Here's the beggining of the dput file of data$complete: c("2001-07-29 17:20:00", "2001-07-03 02:39:00", "2001-08-01 02:53:00", "2001-08-01 17:24:00", "2001-07-19 01:08:00", "2001-02-18 14:20:00", "2001-07-28 20:23:00", "2001-08-16 18:12:00", "2001-09-16 15:40:00", "2001-05-08 14:55:00", "2001-08-04 16:05:00", "2001-08-04 11:00:00", – Hugo Torres Feb 16 '18 at 15:31
  • @HugoTorres This kind of result is commonly associated with an incomplete value somewhere in the data. As @trosendal said, impossible to help you here, without the full data. Try to convert dates inside a loop (or `apply`) and raise an error when the format does not contain hours, minutes and seconds. – bVa Feb 21 '18 at 15:32
  • @bVa meanwhile I was able to solve it, simply by adding the time zone. Don't really know what was wrong to be honest – Hugo Torres Feb 22 '18 at 10:19

0 Answers0