Easiest way to have "yyyy.mm.dd" format in R? - 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')

Related

how I can convert an specific character string to date time in r?

Hi and thanks for reading me. Im trying to convert a character string in to a datetime format in r, but I cannot discover the way to do that, because the year is only taken the last 2 digits ("22" instead of "2022"), im not sure of how to fix It.
The character string is "23/8/22 12:45" and I tried with:
as.Date("23/8/22 12:45", format ="%m/%d/%Y" )
and
as_datetime(ymd_hm("23/8/22 12:45"), format ="%m/%d/%Y")
But it doest work. Anyone knows how I can fix it? thanks for the help
as.Date returns only the calendar date, because class Date objects are only shown as calendar dates.
in addition to POSIXct, you can also use parse_date_time from lubridate package:
library(lubridate)
parse_date_time("23/8/22 12:45", "dmy HM", tz="") #dmy - day, month, year HM - hour, minute
# Or the second function:
dmy_hm("23/8/22 12:45",tz=Sys.timezone())
As has been mentioned in the comments you need to use "%d/%m/%y" to correctly parse the date in your string. But, if you want datetime format (in base r this is normally done with POSIXct classes) you could use as.POSIXct("23/8/22 12:45", format = "%d/%m/%y %H:%M"). This will make sure you keep information about the time from your string.

Unable to convert character / factor class into date

I know this question is asked a lot, but I only come to you because I tried everything (including the tips from similar questions that I managed to understand).
I have a rather big CSV file (> 16 000 rows), with, among others, a "Date" column, containing dates in the following format "01/01/1999".
However, when loading the file, the column is not recognised as a date, but as a Factor with read.csv2, or a character with fread (data.table package). I loaded the lubridate library.
In both cases, I tried to convert the column to a date format, using all methods I knew (column = Date, data = test):
as.Date(test$Date, format = "%d/%m/%Y", tz = "")
Or
strptime(test$Date, format = "%d/%m/%y", tz = "")
Or
as_date(test$Date)
And also the function dmy from lubridate, and
as.POSIXct(test$Date, "%d/%m/%y", tz = "").
I also tried changing the format: ymd instead of dmy, "-" instead of "/".
I even tried to change the character class to numeric (when loaded with fread), and the factor class to numeric (when loaded with read.csv2).
Despite all of this, the columns stay in their factor / character classes.
Does someone know what I missed?
Just use the anydate() function from the anytime package:
R> library(anytime)
R> var <- as.factor(c("01/01/1999", "01/02/1999"))
R> anydate(var)
[1] "1999-01-01" "1999-01-02"
R>
R> class(anydate(var))
[1] "Date"
R>
R> class(var)
[1] "factor"
R>
R>
It will read just about any input time, and convert it without requiring a format and this works as long as the represented is somewhat standard (i.e. we do not work with two-digit years etc).
(Otherwise you can of course also use the base R functions after first converting from factor back to character via as.character(). But anytime() and anydate() do that, and much more, for you too.)
If you are using read.csv2, try
read.csv2(..., stringsAsFactors=F)
and then continue with as.Date

Convert factors to datetime, <fctr> 10/25/2018 (M, D, Y)

I have a data frame called RequisitionHistory2 with a variable called RequisitionDateTime and the levels are factors which look like 4/30/2019 14:16 I would like to split this into RequisitionDate and RequisitionTime in a datetime format.
I tried this code, but this still does not solve my issue with needing to split these into their own columns. The code also did not work as I got the error below.
mutate(When = as.POSIXct(RequisitionHistory2, format="%m/%d/%. %H:%M %p"))
Error in as.POSIXct.default(RequisitionHistory2, format = "%m/%d/%. %H:%M %p") : do not know how to convert 'RequisitionHistory2' to class “POSIXct”
I would like to have the variable RequisitionDateTime split into RequisitionDate and another variable RequisitionTime in the dataframe RequisitionHistory2. Any help is greatly appreciated!
Do not convert factors to datetime directly. You will need to convert it to a character first and then use a datetime function.
as.Date(as.character("10/25/2018"), format = "%m/%d/%Y")
would work for your date example.
library(lubridate)
mutate(df,When = mdy_hm(RequisitionHistory2))
If your datetime is in 4/30/2019 14:16 format
Note that as.POSIXct() works only on datetimes already in ISO 8601 format. I wrote a blog post about this and I think would be helpful for you to check out:
https://jackylam.io/tutorial/uber-data/
The anytime package ON CRAN directly converts from many formats, including factor and ordered to dates and datetime objects. It also heuristically tries a number of viable formats so that you do not need a format string. See the README at GitHub for an introduction, there is also a vignette
Your example works:
R> library(anytime)
R> anytime(as.factor("4/30/2019 14:16"))
[1] "2019-04-30 14:16:00 CDT"
R> anytime(as.factor("4/3/2019 14:16:17"), useR=TRUE)
[1] "2019-04-03 14:16:17 CDT"
R>
However, the underlying (Boost C++) parser does not like single digit days or month so you may need to flip back to R's parser via useR=TRUE as I did on the second example.

Want to convert date from a specific format

I have a list of data that has date information in the format:
11-Feb-08, 13-Feb-08, 2-Mar-08 etc. How can I change all the entries in this column to be in dd/mm/yy format. I have tried as.Date and as.POSIXct but it converts it to NAs. sos pls help.
You are getting NAs for the date values because of the formatting issue. Provide appropriate date format in format argument of as.POSIXct or as.Date function.
As per the date example(11-Feb-08), the appropriate format would be :
format = '%d-%b-%y'.
Do look at the documentation using ?strptime for format related query.It is well documented for each kind of date format.
You can try below Code using lubridate
library(lubridate)
c<-data.frame("Date" = c("11-Feb-08","13-Feb-08", "2-Mar-08"))
c$Date<-dmy(c$Date, tz = "Asia/Kolkata")
str(c$Date)
You will get below result:
POSIXct[1:3], format: "2008-02-11" "2008-02-13" "2008-03-02"

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/

Resources