R strptime issue when using %b to format a date - r

Before marking as duplicate, I've tried a few other solutions, namely these:
R, strptime(), %b, trying to convert character to date format
strptime, as.POSIXct and as.Date return unexpected NA
But neither seem to work for me.
I'm trying to convert a time format Dec-18 to a POSIXct time (would be 2018-12-01 in this case). I'm attempting to use strptime with %b and %y to achieve this as so:
> strptime("Dec-18", format = "%b-%y")
[1] NA
But obviously it is not working. I'm reading a out about "locales" and such, but the above solutions did not work for me. I attempted the following:
> Sys.setlocale("LC_TIME", "C")
[1] "C"
> strptime("Dec-18", format = "%b-%y")
[1] NA
It was also suggested to use this locale, Sys.setlocale("LC_TIME", "en_GB.UTF-8"), but I get an error when trying to use this:
> Sys.setlocale("LC_TIME", "en_GB.UTF-8")
[1] ""
Warning message:
In Sys.setlocale("LC_TIME", "en_GB.UTF-8") :
OS reports request to set locale to "en_GB.UTF-8" cannot be honored
Kind of at a loss for what to do here. My abbreviated months seem right based off this:
> month.abb
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
Here's the version of R that I am running:
R version 3.5.3 (2019-03-11) -- "Great Truth"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
Thanks in advance.

With lubridate, you can do:
parse_date_time("Dec-18", "my")
[1] "2018-12-01 UTC"

The most simplest solution would be
library(zoo)
as.Date(as.yearmon("Dec-18", "%b-%y"))
#[1] "2018-12-01"
The issue in the OP's code is that strptime or as.Date requires a day too. If it is not there, the format is not complete for a Date. One option would be to paste a day in strptime and it works
strptime(paste0("Dec-18", "-01"), format = "%b-%y-%d")
#[1] "2018-12-01 EST"

The simplest solution will be this:
as.Date(x = paste0("01-", "Dec-18"),
format = "%d-%b-%y")
#> [1] "2018-12-01"
format(x = as.Date(x = paste0("01-", "Dec-18"),
format = "%d-%b-%y"),
format = "%b-%y")
#> [1] "Dec-18"
Created on 2019-05-15 by the reprex package (v0.2.1)
R doesn't recognise Dec-18 as date. Add a 01- so that it can detect it as date, and then display as you prefer.

Related

Format date in R with lubridate

My input data, formatted as character, looks like this
"2020-07-10T00:00:00"
I tried
library(lubridate)
mdy_hms("2020-07-10T00:00:00", format='%Y-%m-%dT%H:%M:%S', tz=Sys.timezone())
But I get
[1] NA NA
Warning message:
All formats failed to parse. No formats found.
I tried the more flexibel approach parse_date_time(), but without luck
parse_date_time("2020-07-10T00:00:00", '%Y-%m-%dT%H:%M:%S', tz=Sys.timezone())
How can I convert this date "2020-07-10T00:00:00" to a date R recognizes? Note: I am not interested in the time really, only the date!
Why not just
as.Date("2020-07-10T00:00:00")
# [1] "2020-07-10"
Fun fact:
as.Date("2020-07-101sddT00:1sdafsdfsdf0:00sdfzsdfsdfsdf")
# [1] "2020-07-10"
Assuming that the 07 is the month of July, and the 10 is the 10th:
x <- "2020-07-10T00:00:00"
ymd_hms(x, tz = Sys.timezone())
> [1] "2020-07-10 AEST"
If it's in format year-day-month, swap the ymd for ydm.
Hope this helps!

Abbreviated month issue in formatting the date [duplicate]

Before marking as duplicate, I've tried a few other solutions, namely these:
R, strptime(), %b, trying to convert character to date format
strptime, as.POSIXct and as.Date return unexpected NA
But neither seem to work for me.
I'm trying to convert a time format Dec-18 to a POSIXct time (would be 2018-12-01 in this case). I'm attempting to use strptime with %b and %y to achieve this as so:
> strptime("Dec-18", format = "%b-%y")
[1] NA
But obviously it is not working. I'm reading a out about "locales" and such, but the above solutions did not work for me. I attempted the following:
> Sys.setlocale("LC_TIME", "C")
[1] "C"
> strptime("Dec-18", format = "%b-%y")
[1] NA
It was also suggested to use this locale, Sys.setlocale("LC_TIME", "en_GB.UTF-8"), but I get an error when trying to use this:
> Sys.setlocale("LC_TIME", "en_GB.UTF-8")
[1] ""
Warning message:
In Sys.setlocale("LC_TIME", "en_GB.UTF-8") :
OS reports request to set locale to "en_GB.UTF-8" cannot be honored
Kind of at a loss for what to do here. My abbreviated months seem right based off this:
> month.abb
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
Here's the version of R that I am running:
R version 3.5.3 (2019-03-11) -- "Great Truth"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
Thanks in advance.
With lubridate, you can do:
parse_date_time("Dec-18", "my")
[1] "2018-12-01 UTC"
The most simplest solution would be
library(zoo)
as.Date(as.yearmon("Dec-18", "%b-%y"))
#[1] "2018-12-01"
The issue in the OP's code is that strptime or as.Date requires a day too. If it is not there, the format is not complete for a Date. One option would be to paste a day in strptime and it works
strptime(paste0("Dec-18", "-01"), format = "%b-%y-%d")
#[1] "2018-12-01 EST"
The simplest solution will be this:
as.Date(x = paste0("01-", "Dec-18"),
format = "%d-%b-%y")
#> [1] "2018-12-01"
format(x = as.Date(x = paste0("01-", "Dec-18"),
format = "%d-%b-%y"),
format = "%b-%y")
#> [1] "Dec-18"
Created on 2019-05-15 by the reprex package (v0.2.1)
R doesn't recognise Dec-18 as date. Add a 01- so that it can detect it as date, and then display as you prefer.

Getting NA when using as.Date()

Thanks to comments below, I realized I should use "%b" for "FEB" (originally I used "%m"; thanks for the reference to ?strptime). But my problem still stands.
When I do
as.Date("13-FEB-15", "%d-%b-%y")
# [1] NA
I know this will work:
as.Date("13-02-2015", "%d-%m-%Y")
# [1] "2015-02-13"
But is there a way to avoid converting FEB to 02 and 15 to 2015 in order to get my expected result? Thanks!
A general and useful diagnostic
Try this and what do you get?
format(strptime(Sys.Date(), format="%Y-%m-%d"), "%y-%b-%d")
I got
[1] "16- 7月-22"
Haha, the middle one is Chinese. So what is going wrong? Nothing wrong. The issue is that %b is sensitive to your current locale. When you read ?strptime, pay special attention to what format is sensitive to your current locale.
My locale is:
Sys.getlocale("LC_TIME")
#[1] "zh_CN.UTF-8"
Yep, that is in China region.
Locales make a difference in Date-Time format. On my machine:
as.Date("16-JUL-22", "%y-%b-%d")
# NA
as.Date("16- 7月-22", "%y-%b-%d")
#[1] "2016-07-22"
Now let's reset time locale:
Sys.setlocale("LC_TIME", "C")
as.Date("16-JUL-22", "%y-%b-%d")
#[1] "2016-07-22"
Wow, it works! Read ?locales for more, and you will understand what locale = "C" means.
Solution for you
Sys.setlocale("LC_TIME", "C")
as.Date("13-FEB-15", format = "%d-%b-%y")
Using lubridate:
library(lubridate)
date1 = "2014-12-11 00:00:00"
date2 = "14-DEC-11"
ymd_hms(date1) == ymd(date2,tz = "UTC")
These equal each other and should be able to be joined.

R - format of a date

I am facing an issue with formatting of date and cannot find a solution. Here is the code - the second date becomes not the format like I want to.
date1
#[1] "01. Nov 11"
ndate1 <- as.Date(date1, "%d. %B %y")
ndate1
#[1] "2011-11-01"
date2
#[1] "26-May-13"
ndate2 <- as.Date(date2, "%d-%B-%y")
ndate2
#[1] NA
You can determine the complete or abbreviated month names in your locale using the example on the ?Constants page:
format(ISOdate(2000, 1:12, 1), "%b")
Per ?strptime on input you can use either "%B" or "%b" for either abbreviated or complete names.
This is most probably due to an incompatibility with the locale settings. If the output of Sys.getlocale("LC_TIME") does not correspond to an English setting, like "en_US.UTF-8" or "en_GB.UTF-8", the abbreviation "May" (which, coincidentally, is not even an abbreviation in this case) is not recognized in most (all?) other settings. In contrast, "Nov" is a valid abbreviation for the month of November in several languages. This might explain why the first case with date1 does not cause trouble.
We could try this:
Sys.setlocale("LC_TIME", "en_US.UTF-8")
date2 <- "26-May-13"
ndate2 <- as.Date(date2, "%d-%b-%y")
ndate2
#[1] "2013-05-26"

why is as.Date("06/10/2013", "%m/%d/%y") returning the year 2020?

Like the title says,
>as.Date("06/10/2013", "%m/%d/%y") # returns "2020-06-2013
[1] "2020-06-10"
What is causing this?
I'm using windows with 32-bit RGui and the latest zoo package.
Thanks.
Because %y is not %Y:
R> as.Date("06/10/2013", "%m/%d/%Y") # note capital-Y
[1] "2013-06-10"
R>

Resources