Convert string into date format in R - r

I'm trying to convert a string into a date format "yyy-mm-dd" in R programming using below steps:
date<- as.Date(S, "%Y- %m- %d")
where S is a vector containing a string value "15082022"
But above method is not giving an output in the format yyy-mm-dd.
Can someone advise the correct command for this

The as.Date() function will convert a string into date format, and the format of the output will always be in yyyy-mm-dd format in R (ISO 8601). The format argument in the as.Date() function is to specify the date format of the string input. I remember I initially thought it was specifying the output format, but it's the input format (you can change the output format with a subsequent format() function, however this will convert it back to a string).
Your string looks to be in ddmmyyyy (%d%m%Y) format, this should be what you specify as the format argument in as.Date(). Your format does not include hyphens, so the format argument should also not include hyphens. Note that ddmmyyyy, dd-mm-yyyy, dd/mm/yyyy, dd.mm.yyyy are all different date formats, even though the day, month and year are in the same order, they would be converted to date with formats %d%m%Y, %d-%m-%Y, %d/%m/%Y, and %d.%m.%Y, respectively.
Further advice on working with dates and times is available in the relevant chapters of R for Data Science by Wickham & Grolemund and The R Cookbook by Teetor & Long.

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.

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"

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.

read.csv2 date formatting in R

I wish to import my csv file into a data frame but the date in my csv file is in a non-standard format.
The date in the first column is in the following format:
08.09.2016
One of the arguments in my read.csv2 functions is to specify the classes and when I specify this column as a date I receive the following error upon execution:
Error in charToDate(x) :
character string is not in a standard unambiguous format
I'm guessing it doesn't like converting the date from factor class to date class.
I've read a little about POSIXlt but I don't understand the details of the function.
Any ideas how to convert the class from factor to date??
When you convert character to date, you need specify format if it is not standard. The error you got is the result of as.Date("08.09.2016"). But if you do as.Date("08.09.2016", format = "%m.%d.%Y"), it is fine.
I am not sure whether it is possible to pass format to read.csv2 for correct date formatting (maybe not). I would simply read in this date column as factor, then do as.Date(as.character(), format = "%m.%d.%Y") on this column myself.
Generally we use the following format "dd/mm/yy" how can I reorganise the date to that format?
Use format(, format = "%d/%m/%y").
A complete example:
format(as.Date("08.09.2016", format = "%m.%d.%Y"), format = "%d/%m/%y")
# [1] "09/08/16"

How can we convert a character string such as "2007-11-31 13:15:54" to a as.Date format in R?

I have a data table with a character string read in using the fread option in the data.table package. As such, it converts "2007-11-31 13:15:54" into a character string. The string "2007-11-31 13:15:54" represents "%Y-%m-%d %hh-%mm-%ss" in date notation.
I am trying to convert this type of character string into a Date format. I am using the as.Date function in R.
My first try is to do:
char.string <- "2007-11-31 13:15:54"
convert <- as.Date(char.string, "%Y-%m-%d %hh-%mm-%ss")
However, all I get when I return convert is NA.
Is there some way to convert this type of character string? Thanks!
Looks like I found the answer online. Instead of as.Date, use ymd_hms() under the package lubridate.

Resources