Resolving "Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format" error msg

I am trying to split a column of date timestamp data into two individual columns. I have several columns that have date timestamp info. The first one, I was able to use the following and it worked with no problem:

delivered$order_date<-as.Date(delivered$order_purchase_timestamp)
delivered$order_time<-format(as.POSIXct(delivered$order_purchase_timestamp),
format = "%H:%M:%S")

The "order_purchase_timestamp" column is what I split into two separate columns called order_date and order_time. Again, this worked with no problem.

However, when I tried to split three other columns that had date timestamp info combined, I get this error:
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format

This seems to be only happening for the timestamp portion of the column; the "date" portion splits into its own column with no problem.

I watched a YouTube video on how to fix the error (convert the character string to a numeric string) but the example did NOT cover how to do this with a column of date timestamp data. This is the code the video suggested I use:
date<-as.numeric(as.character(date)), where "date" would be order_approved_numeric for my purposes. I entered the following code to fix my problem:

#to fix character string error msg, must convert character string to numeric
approved_time_numeric <- as.numeric(as.character(delivered$order_approved_at))
#order_approved_at is the column in the dataset that contains the date timestamp data

I got this error: Warning message:
NAs introduced by coercion

I then ran the entire code, both for the date and for the timestamp:
delivered$approved_date<-as.Date(delivered$order_approved_at)
delivered$approved_time<-format(as.POSIXct(approved_time_numeric),
format = "%H:%M:%S")

When I look at the revised dataset, it shows a new column called approved_time but it just has NA all the way down the column.

Any suggestions and advice is greatly appreciated.

Can you paste some example data using dput(head(yourdf, 10))? How would you like the final result to be?

Will this work? As you can see, the order_date and order_time columns are good; I split this from another column not seen in the image. But you can get a good idea of what the date timestamp data looks like by looking at the order_estimated_delivery_date column, which is another column I need to split but am having the same issue with.

Run the code I provided in the previous post and copy and paste here the output

dput(head(delivered, 10))
structure(list(order_status = c("delivered", "delivered", "delivered",
"delivered", "delivered", "delivered", "delivered", "delivered",
"delivered", "delivered"), order_purchase_timestamp = c("2017-10-02 10:56:33",
"2018-07-24 20:41:37", "2018-08-08 08:38:49", "2017-11-18 19:28:06",
"2018-02-13 21:18:39", "2017-07-09 21:57:05", "2017-05-16 13:10:30",
"2017-01-23 18:29:09", "2017-07-29 11:55:02", "2017-05-16 19:41:10"
), order_approved_at = c("2017-10-02 11:07:15", "2018-07-26 03:24:27",
"2018-08-08 08:55:23", "2017-11-18 19:45:59", "2018-02-13 22:20:29",
"2017-07-09 22:10:13", "2017-05-16 13:22:11", "2017-01-25 02:50:47",
"2017-07-29 12:05:32", "2017-05-16 19:50:18"), order_delivered_carrier_date = c("2017-10-04 19:55:00",
"2018-07-26 14:31:00", "2018-08-08 13:50:00", "2017-11-22 13:39:59",
"2018-02-14 19:46:34", "2017-07-11 14:58:04", "2017-05-22 10:07:46",
"2017-01-26 14:16:31", "2017-08-10 19:45:24", "2017-05-18 11:40:40"
), order_delivered_customer_date = c("2017-10-10 21:25:13", "2018-08-07 15:27:45",
"2018-08-17 18:06:29", "2017-12-02 00:28:42", "2018-02-16 18:17:02",
"2017-07-26 10:57:55", "2017-05-26 12:55:51", "2017-02-02 14:08:10",
"2017-08-16 17:14:30", "2017-05-29 11:18:31"), order_estimated_delivery_date = c("2017-10-18 00:00:00",
"2018-08-13 00:00:00", "2018-09-04 00:00:00", "2017-12-15 00:00:00",
"2018-02-26 00:00:00", "2017-08-01 00:00:00", "2017-06-07 00:00:00",
"2017-03-06 00:00:00", "2017-08-23 00:00:00", "2017-06-07 00:00:00"
), order_date = structure(c(17441, 17736, 17751, 17488, 17575,
17356, 17302, 17189, 17376, 17302), class = "Date"), order_time = c("10:56:33",
"20:41:37", "08:38:49", "19:28:06", "21:18:39", "21:57:05", "13:10:30",
"18:29:09", "11:55:02", "19:41:10"), approved_date = structure(c(17441,
17738, 17751, 17488, 17575, 17356, 17302, 17191, 17376, 17302
), class = "Date"), approved_time = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_)), row.names = c(NA,
10L), class = "data.frame")

This should solve

library(tidyverse)
library(lubridate)
library(hms)

df <- structure(list(order_status=c("delivered","delivered","delivered",
"delivered","delivered","delivered","delivered","delivered",
"delivered","delivered"),order_purchase_timestamp=c("2017-10-0210:56:33",
"2018-07-2420:41:37","2018-08-0808:38:49","2017-11-1819:28:06",
"2018-02-1321:18:39","2017-07-0921:57:05","2017-05-1613:10:30",
"2017-01-2318:29:09","2017-07-2911:55:02","2017-05-1619:41:10"
),order_approved_at=c("2017-10-0211:07:15","2018-07-2603:24:27",
"2018-08-0808:55:23","2017-11-1819:45:59","2018-02-1322:20:29",
"2017-07-0922:10:13","2017-05-1613:22:11","2017-01-2502:50:47",
"2017-07-2912:05:32","2017-05-1619:50:18"),order_delivered_carrier_date=c("2017-10-0419:55:00",
"2018-07-2614:31:00","2018-08-0813:50:00","2017-11-2213:39:59",
"2018-02-1419:46:34","2017-07-1114:58:04","2017-05-2210:07:46",
"2017-01-2614:16:31","2017-08-1019:45:24","2017-05-1811:40:40"
),order_delivered_customer_date=c("2017-10-1021:25:13","2018-08-0715:27:45",
"2018-08-1718:06:29","2017-12-0200:28:42","2018-02-1618:17:02",
"2017-07-2610:57:55","2017-05-2612:55:51","2017-02-0214:08:10",
"2017-08-1617:14:30","2017-05-2911:18:31"),order_estimated_delivery_date=c("2017-10-1800:00:00",
"2018-08-1300:00:00","2018-09-0400:00:00","2017-12-1500:00:00",
"2018-02-2600:00:00","2017-08-0100:00:00","2017-06-0700:00:00",
"2017-03-0600:00:00","2017-08-2300:00:00","2017-06-0700:00:00"
),order_date=structure(c(17441,17736,17751,17488,17575,
17356,17302,17189,17376,17302),class="Date"),order_time=c("10:56:33",
"20:41:37","08:38:49","19:28:06","21:18:39","21:57:05","13:10:30",
"18:29:09","11:55:02","19:41:10"),approved_date=structure(c(17441,
17738,17751,17488,17575,17356,17302,17191,17376,17302
),class="Date"),approved_time=c(NA_character_,NA_character_,
NA_character_,NA_character_,NA_character_,NA_character_,NA_character_,
NA_character_,NA_character_,NA_character_)),row.names=c(NA,
10L),class="data.frame") %>% as_tibble()


# split order_purchase_timestamp
df$order_purchase_timestamp_splitdate <- as_date(df$order_purchase_timestamp)
df$order_purchase_timestamp_splittime <- as_hms(as_datetime(df$order_purchase_timestamp))

# split order_approved_at
df$order_approved_at_splitdate <- as_date(df$order_approved_at)
df$order_approved_at_splittime <- as_hms(as_datetime(df$order_approved_at))

# split order_delivered_carrier_date
df$order_delivered_carrier_date_splitdate <- as_date(df$order_delivered_carrier_date)
df$order_delivered_carrier_date_splittime <- as_hms(as_datetime(df$order_delivered_carrier_date))

# split order_delivered_customer_date
df$order_delivered_customer_date_splitdate <- as_date(df$order_delivered_customer_date)
df$order_delivered_customer_date_splittime <- as_hms(as_datetime(df$order_delivered_customer_date))

# split order_estimated_delivery_date
df$order_estimated_delivery_date_splitdate <- as_date(df$order_estimated_delivery_date)
df$order_estimated_delivery_date_splittime <- as_hms(as_datetime(df$order_estimated_delivery_date))


1 Like

That worked! Thanks so much! So, I'm assuming I need to use lubridate. I'll need to do more research into what all it does and when to use it. Thanks again for your help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.