Character String Is Not in a Standard Unambiguous Format

What are the standard unambiguous date formats for string-to-date conversion in R?

This is documented behavior. From ?as.Date:

format: A character string. If not specified, it will try
'"%Y-%m-%d"' then '"%Y/%m/%d"' on the first non-'NA' element,
and give an error if neither works.

as.Date("01 Jan 2000") yields an error because the format isn't one of the two listed above. as.Date("01/01/2000") yields an incorrect answer because the date isn't in one of the two formats listed above.

I take "standard unambiguous" to mean "ISO-8601" (even though as.Date isn't that strict, as "%m/%d/%Y" isn't ISO-8601).

If you receive this error, the solution is to specify the format your date (or datetimes) are in, using the formats described in the Details section in ?strptime.

Make sure that the order of the conversion specification as well as any separators correspond exactly with the format of your input string. Also, be sure to use particular care if your data contain day/month names and/or abbreviations, as the conversion will depend on your locale (see the examples in ?strptime and read ?LC_TIME; see also strptime, as.POSIXct and as.Date return unexpected NA).

character string is not in a standard unambiguous format

It might be that you have a character or factor, and it's expecting a numeric vector for conversion from unix time :

as.POSIXct(as.numeric(as.character(df3$deadline)),origin="1970-01-01")

R: Error in charToDate(x) : character string is not in a standard unambiguous format

Here is a dplyr approach with mutate across:

library(dplyr)

df %>%
mutate(across(c(switching_home, between_sentence_sw_home, within_sentence_sw_home), ~case_when(. == "Always" ~ 100 ,
. == "Frequently" ~ 75,
. == "Most of the time" ~ 75,
. == "Sometimes" ~ 50,
. == "Rarely" ~ 25,
. == "Never" ~ 0,
TRUE ~ NA_real_))
)
         DOB date_today_ppt_SEEQ switching_home single_lang_environm_home dual_lang_environm_home dense_code_sw_home between_sentence_sw_home
1 2019-09-16 2019-09-16 50 80 20 0 50
2 2019-09-25 2019-09-25 75 0 60 40 75
3 2021-06-07 2021-06-07 50 100 0 0 50
4 2020-03-07 2020-03-07 50 75 23 2 50
5 2020-02-22 2020-02-22 25 95 0 5 0
6 2020-02-15 2020-02-15 50 70 20 10 50
7 2020-03-03 2020-03-03 25 30 70 0 25
within_sentence_sw_home
1 50
2 75
3 75
4 25
5 25
6 25
7 50

the string is not in an unambiguous standard format

You can use order on the original dates to get the correct order.

First, read in your data example.

Date <- scan(what = character(), text = '
2015-07-31
2013-01-12
2014-01-03
2014-12-03
2013-11-13
2013-10-27')
Date <- as.Date(Date)

Now, reformat and order them.

inx <- order(Date)
newDate <- format(Date, format = "%B_%Y")[inx]
newDate
#[1] "January_2013" "October_2013" "November_2013" "January_2014"
#[5] "December_2014" "July_2015"

Note that this assumes that the dates are of class Date.



Related Topics



Leave a reply



Submit