Problem to convert string with month abbreviation to POSIXlt [duplicate] - r

This question already has answers here:
strptime, as.POSIXct and as.Date return unexpected NA
(2 answers)
Closed 2 years ago.
I tried to convert Excel date character to date-time class using POSIXlt. This is an example of my data: "01:56:00 06-Apr-2017".
For the format, I used the character string giving a date-time format as used by strptime.
I tried as.POSIXlt(new_dtime, format = "%H:%M:%S %d-%b-%Y"), but it resulted in a bunch of NA. I am sure that the problem is related to the month abbreviation, despite I used %b as strptime suggests. Any help?

I'm going to take a guess that this is a locale problem: from ?strptime, %b denotes "[a]bbreviated month name in the current locale on this platform" (emphasis added). "Apr" is the abbreviation for April in an English locale. This question suggests the following solution:
str1 <- "01:56:00 06-Apr-2017"
orig_locale <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME", "C")
as.POSIXct(str1, format = "%H:%M:%S %d-%b-%Y")
Sys.setlocale("LC_TIME", orig_locale)

Related

Subset month, day, hour, minute, second from a date [duplicate]

This question already has answers here:
strptime, as.POSIXct and as.Date return unexpected NA
(2 answers)
Changing date format in R
(7 answers)
Closed 1 year ago.
I have a column in a dataframe which contains dates eg:
Date <- as.Date("2012-12-01 00:00:00")
Note: the actual dataframe format is "unknown"
I want to subset the month, date, hour from this dataframe and used this code
dmH <- as.POSIXct(Date, format="%Y%m%d %H%M%s")
dmH <- format(dmH, format="%m%d %H%M%s")
which returns a character format as below
"1201 02001354320000"
During this process it changed from UTC to EET so it starts at 02:00:00 and I don't know how to omit this change.
Most importantly, I need to have it in date format to be able to use it in a ggplot but I wasn't able to find any way to convert it, no matter what and how I tried.
EDIT:
As #Cath mentioned in the comment I tried to use that code but as.Date function returns only the year, month, day without the time. As a result, when I then try format function for any other time of the day it returns "00".
As opposed to as.Date I used again as.POSIXct and now it returns the right format (since I used the hyphen and %S in the "format" argument as you recommended). But still this is in character format which I need in date format.
So I used again mdH <- as.POSIXct(mdH, format = "%m-%d %H:%M:%S") on the formatted dataframe(mdH) as well as strptime to change it to date format but both return also the current year.
Note that if I use directly dmH <- strptime(as.character(Date), format="%m-%d %H:%M:%S") (as in one of the threads you recommended) it returns NA. Am I missing something? I can't resolve my issue

Lubridate Date parsing is one year off [duplicate]

This question already has answers here:
date functions in R return wrong year
(2 answers)
Closed 3 years ago.
Within R, I'm trying to convert a text string into a Date variable type using lubridate's as.Date function.
I have a vector of values such as:
Dates
11/28/2019
11/29/2019
I am attempting to convert these to standard date variables using this as.Date function:
as.Date(Dates, "%m/%d/%y")
I do not receive an error message, and it correctly interprets the month and date, but for some reason it's outputting the wrong year - one year ahead:
"2020-11-28"
"2020-11-29"
I have no earthly idea why it is incorrectly interpreting the year in this way. Any help is appreciated!
We need to use %Y for 4 digit year as %y refers to only 2 digit
as.Date(Dates, "%m/%d/%Y")
Or using lubridate, this would be resolved
library(lubridate)
mdy(Dates)
Or with anydate from anytime
library(anytime)
anydate(Dates)

How to convert character in yyyy-mm format to date r [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Get the difference between dates in terms of weeks, months, quarters, and years
(9 answers)
Closed 4 years ago.
I transformed a date in the yyyy-mm-dd format to the yyyy-mm format using the following command:
format(as.Date(DATE, "%Y-%m-%d"), "%Y-%m")
This works but it returns a character. I want to further use this as a date, so I want to transform this character back to a date class. Using the as.Date() function gives me the error:
Error in charToDate(x) : character string is not in a standard unambiguous format
Does anyone know how to solve this problem?
Update: In the end I want to determine the number of months between two dates in the format yyyy-mm. Does anyone knows how to do this without transforming the characters back to date class?
I had once the same problem and unfortunately the only solution I found was to keep format YYYY-mm-dd with dd==01...
Here is part of the code if you want:
DATE <- str_c(DATE,"-01") df$date <- as.Date(DATE,format="%Y-%m-%d")

how to covert dates with just year-month into year-month-date in R? [duplicate]

This question already has an answer here:
Generating a date from a string with a 'Month-Year' format
(1 answer)
Closed 4 years ago.
I have a list of program dates as character strings in the following format
program.date.have <-c('Sep-14','Aug-14','Sep-16')
I am assuming that all these programs started on the first day of each month, and I want the program.date to end up like
program.date.want<-c('2014-09-01', '2014-08-01, '2016-09-01') or in YYYY-MM-DD format.
To start somewhere I have decided to covert the character strings into the date format in the following way
program.date.have<-c('Sep-14','Aug-14','Sep-16')
betterDates <- as.Date(program.date,
format = "%m-%y")
But even that does not seem to work. how do I use values in program.date variable to be converted into format I want in program.date.want
We can use as.yearmon from zoo, specify the format, and wrap with as.Date which automatically generates the 'day' as the first of the month.
library(zoo)
as.Date(as.yearmon(program.date.have, "%b-%y"))
#[1] "2014-09-01" "2014-08-01" "2016-09-01"
Or a base R option is to paste the '01' at the start or end and then specify the appropriate format in as.Date
as.Date(paste0(program.date.have, "-01"), "%b-%y-%d")
#[1] "2014-09-01" "2014-08-01" "2016-09-01"

Convert string to Date in R: month abbreviated "dd-mmm-YYYY" [duplicate]

This question already has answers here:
strptime, as.POSIXct and as.Date return unexpected NA
(2 answers)
Closed 4 years ago.
I have a vector of strings which I need to convert to dates. This only works for some of the dates which are in the format of "dd-mmm-YYYY".
Example: this works: strptime("21-Sep-2017", format = "%d-%b-%Y")
This does not work and returns NA: strptime("21-Dec-2017", format = "%d-%b-%Y")
What am I doing wrong or not seeing?
This is because your locale is probably one where December is not abbreviated as Dec. Without changing your session settings, you could simply do
lubridate::parse_date_time("21-Dec-2017", orders = "d-b-Y", locale = "us")
[1] "2017-12-21 UTC"

Resources