date format without leading zeros - R [duplicate] - r

This question already has answers here:
Formatting a date in R without leading zeros
(4 answers)
Closed 2 years ago.
I have dates in a character vector and I'm looking for a way to transform it to date format with as.Date():
vector <- c("15/1/2019", "5/5/2019")
However, as.Date(vector, format="%d/%m/%Y") receives as input characters with zeros leading. And I have found plenty of solutions for:
Input: "15/01/2019"
Output: "15/1/2019"
However, I need exactly the opposite to make as.Date() to work:
Input: "15/1/2019" "5/5/2019"
Output: "15/01/2019" "05/05/2019"
Is there any specific formatting for format argument? Or I have to use a substr() solution?

We can use strftime.
strftime(as.Date(inp, format="%d/%m/%Y"), format="%d/%m/%Y")
# [1] "15/01/2019" "05/05/2019"
Data
inp <- c("15/1/2019", "5/5/2019")

I'm not sure I understand the issue. as.Date() works as expected on your input:
vector <- c("15/1/2019", "5/5/2019")
dates <- as.Date(vector, format = "%d/%m/%Y")
dates
[1] "2019-01-15" "2019-05-05"
To get the desired output, you use format:
format(dates, "%d/%m/%Y")
[1] "15/01/2019" "05/05/2019"
EDIT
From your comments, it's clear that you are confusing two different things.
To get a Date object using as.Date(), you need to tell it the format of the input date character string. You have "day-month-year". So you supply that to as.Date():
as.Date(vector, format = "%d/%m/%Y")
The result will always be displayed in the console as year-month-day (YYYY-MM-DD).
Displaying the date in a chosen format is something different. Now we are formatting the Date object back to a character string:
format(dates, "%d/%m/%Y")
To summarise: a properly-converted Date object will always display in the console as YYYY-MM-DD. Formatting that object as a string is a different operation.
I hope this clears things up.

Related

Easiest way to have "yyyy.mm.dd" format in R?

So I realized that this isn't a common date type to deal with at least with using as.Date(). When I do the following , the output isn't correct.
> as.Date(Sys.Date(), format = "yyyy.mm.dd")
[1] "2022-06-21"
Is there an easy way to this with lubridate or base R?
We can use format instead of as.Date as Sys.Date() is already in Date class, however as commented, format returns only a character class
format(Sys.Date(), '%Y.%m.%d')

as.Date() not giving desired result. (giving NA)

I read a .xlsx file containing Date columns into R and converted it into dataframe.
Some date columns are being read correctly but most of the others are getting converted to "43116" format.
Any attempt to convert it into Date using as.Date(, origin= <>, format=<>) is returning NA.
I have tried all possible solutions like using 'stringAsFactors = FALSE', POSIT thing and checking the excel file for date formats but nothing worked.
Please help.
It is difficult to recreate the problem if no data is provided, but if you want to convert the number 43129 or the character "43129" to a R date you should do the following:
a <- 43129
b <- '43129'
format(as.Date(a, origin = "1899-12-30"), '%Y-%m-%d')
[1] "2018-01-29"
format(as.Date(as.integer(b), origin = "1899-12-30"), '%Y-%m-%d')
[1] "2018-01-29"
I used the format yyyy-mm-dd, but any other date format could be used if you format it properly.
Hope it helps!

how to convert date like 09-Oct-2017 to 09-11-2017 by posixct? [duplicate]

This question already has answers here:
Convert R character to date [duplicate]
(1 answer)
Change date format in a list
(1 answer)
Closed 4 years ago.
I have a following datasets
I want to convert these rows in date datatype by using Posixct
right now I am using
as.POSIXct(q3_dos_log$INSERTED_ON , format=c("%d-%mm-%Y"))
but giving wrong output, Kindly solve this.
lubridate library is your friend, which can parse many types of dates automatically (if it doesn't, you can specify what format the data is in, to help the package figure it out).
library(lubridate)
x = "28-Sep-2017"
as.POSIXct(parse_date_time(x, "dmy"))
There is really no need for lubridate here in my opinion; just use base R's as.POSIXct:
as.POSIXct("09-Oct-2017", format = "%d-%b-%Y")
#[1] "2017-10-09 AEDT"
Reformat as "09-10-2017" string:
format(as.POSIXct("09-Oct-2017", format = "%d-%b-%Y"), format = "%d-%m-%Y")
# [1] "09-10-2017"
We could use anydate from anytime to pick up the formats automatically
anytime::anydate("09-Oct-2017")
#[1] "2017-10-09"
All you need is as.Date in Base R and knowledge of the formatting options for dates:
as.Date("07-May-2017", format = "%d-%B-%Y")
See more here: https://www.google.se/amp/s/www.r-bloggers.com/date-formats-in-r/amp/

subset data based on y/m/d/h and get error ouputs [duplicate]

I have a dataset called EPL2011_12. I would like to make new a dataset by subsetting the original by date. The dates are in the column named Date The dates are in DD-MM-YY format.
I have tried
EPL2011_12FirstHalf <- subset(EPL2011_12, Date > 13-01-12)
and
EPL2011_12FirstHalf <- subset(EPL2011_12, Date > "13-01-12")
but get this error message each time.
Warning message:
In Ops.factor(Date, 13- 1 - 12) : > not meaningful for factors
I guess that means R is treating like text instead of a number and that why it won't work?
Well, it's clearly not a number since it has dashes in it. The error message and the two comments tell you that it is a factor but the commentators are apparently waiting and letting the message sink in. Dirk is suggesting that you do this:
EPL2011_12$Date2 <- as.Date( as.character(EPL2011_12$Date), "%d-%m-%y")
After that you can do this:
EPL2011_12FirstHalf <- subset(EPL2011_12, Date2 > as.Date("2012-01-13") )
R date functions assume the format is either "YYYY-MM-DD" or "YYYY/MM/DD". You do need to compare like classes: date to date, or character to character. And if you were comparing character-to-character, then it's only going to be successful if the dates are in the YYYYMMDD format (with identical delimiters if any delimiters are used).
The first thing you should do with date variables is confirm that R reads it as a Date. To do this, for the variable (i.e. vector/column) called Date, in the data frame called EPL2011_12, input
class(EPL2011_12$Date)
The output should read [1] "Date". If it doesn't, you should format it as a date by inputting
EPL2011_12$Date <- as.Date(EPL2011_12$Date, "%d-%m-%y")
Note that the hyphens in the date format ("%d-%m-%y") above can also be slashes ("%d/%m/%y"). Confirm that R sees it as a Date. If it doesn't, try a different formatting command
EPL2011_12$Date <- format(EPL2011_12$Date, format="%d/%m/%y")
Once you have it in Date format, you can use the subset command, or you can use brackets
WhateverYouWant <- EPL2011_12[EPL2011_12$Date > as.Date("2014-12-15"),]

Converting integer format date to double format of date

I have date format in following format in a data frame:
Jan-85
Apr-99
1-Nov
Feb-96
When I see the typeof(df$col) I get the answer as "integer".
Actually when I see the format in excel it is in m/d/yyyy format. I was trying to convert this to date format in R. All my efforts yielded NA.
I tried parse_date_time function. I tried as.date along with as.character. I tried as.POSIXct but everything is giving me NA.
My trials were as follows and everything was a failure:
as.Date.numeric(df$col,"m%d%Y")
transform(df$col, as.Date(as.character(df$col), "%m%d%Y"))
as.Date(df$col,"m%d%Y")
as.POSIXct.numeric(as.character(loan_new$issue_d), format="%Y%m%d")
as.POSIXct.date(as.character(df$col), format="%Y%m%d")
mdy(df$col)
parse_date_time(df$col,c("mdy"))
How can I convert this to date format? I have used lubridate package for parse_date_time and mdy package.
dput output is below
Label <- factor(c("Apr-08",
"Apr-09", "Apr-10", "Apr-11", "Aug-07", "Aug-08", "Aug-09", "Aug-10",
"Aug-11", "Dec-07", "Dec-08", "Dec-09", "Dec-10", "Dec-11", "Feb-08",
"Feb-09", "Feb-10", "Feb-11", "Jan-08", "Jan-09", "Jan-10", "Jan-11",
"Jul-07", "Jul-08", "Jul-09", "Jul-10", "Jul-11", "Jun-07", "Jun-08",
"Jun-09", "Jun-10", "Jun-11", "Mar-08", "Mar-09", "Mar-10", "Mar-11",
"May-08", "May-09", "May-10", "May-11", "Nov-07", "Nov-08", "Nov-09",
"Nov-10", "Nov-11", "Oct-07", "Oct-08", "Oct-09", "Oct-10", "Oct-11",
"Sep-07", "Sep-08", "Sep-09", "Sep-10", "Sep-11"))
NA is typically what you get when you misspecify the format. Which is what you do. That said, if your data is really looking like the first example you gave, it's impossible to simply convert this to a date. You have two different formats, one being month-year and the other day-month.
If your updated date (i.e. Dec-11) is the correct format, then you use the format argument of as.Date like this:
date <- "Dec-11"
as.Date(date, format = "%b-%d")
# [1] "2017-12-11"
Or on your example data:
as.Date(Label, format = "%b-%d")
# [1] "2017-04-08" "2017-04-09" "2017-04-10" "2017-04-11" "2017-08-07" "2017-08-08"
# [7] "2017-08-09" "2017-08-10" "2017-08-11" "2017-12-07" "2017-12-08" "2017-12-09"
If you want to convert something like Jan-85, you have to decide which day of the month that date should have. Say we just take the first of each month, then you can do:
x <- "Jan-85"
xd <- paste0("1-",x)
as.Date(xd, "%d-%b-%y")
# [1] "1985-01-01"
More information on the format codes can be found on ?strptime
Note that R will automatically add this year as the year. It has to, otherwise it can't specify the date. In case you do not have a day of the month (eg like Jan-85), conversion to a date is impossible because the underlying POSIX algorithms don't have all necessary information.
Also keep in mind that this only works when your locale is set to english. Otherwise you have a big chance your OS won't recognize the month abbreviations correctly. To do so, do eg:
Sys.setlocale(category = "LC_TIME", locale = "English_United Kingdom")
You can later set it back to the original one if you must, or restart your R session to reset the locale settings.
note: Please check carefully which locale notations are valid for your OS. The above example works on Windows, but is not guaranteed on either Linux or Mac.
Why you see integer
The fact that these string values are of integer type, is due to the fact that R automatically convert character vectors to factors when reading in a data frame. So typeof() returns integer because that's the internal representation of a factor.

Resources