Getting Wrong date format as result in R - r

I am trying to separate a date from a column in a database, but the result date format is not proper.
column data = "24-01-2021 19:15"
Code used:
database_1$date <- format(as.Date(database_1$start_time), "%d-%m-%Y")
Result: 20-01-0024
Expected result: 24-01-2021

Update:
I just realized the expected output:
just add format("%d.%m.%Y") to the code:
as.Date(dmy_hm(column_date)) %>%
format("%d.%m.%Y")
[1] "24.01.2021"
With lubridate package you can:
With dmy_hm you read in the character column_date to date format
Then you can add as.Date
library(lubridate)
column_date <- "24-01-2021 19:15"
as.Date(dmy_hm(column_date))
[1] "2021-01-24"

Related

R function from string cell YEARMonth as date?

So I have this long dataset, where in one column I have a date specified as character in format YYYMMM, but month abbreviated. So for example 1995MAR, 1995APR and so on. How can I transform that to date format?
I tried as.Date but it obviously hasn't worked, and with lubridate::ymd which hasn't worked as well.
Using parse_date_time from lubridate
date <- "1995MAR"
library(lubridate)
parse_date_time(date, order = "Yb")
Output:
[1] "1995-03-01 UTC"
Alternatively using zoo
library(zoo)
as.Date(as.yearmon(date, '%Y%b'))
Output:
"1995-03-01"
str(as.Date(as.yearmon(date, '%Y%b')))
Date[1:1], format: "1995-03-01"
In Base R, add a day number to parse:
date <- "1995MAR"
as.Date(paste(date, "01"), format = "%Y%b %d")
#[1] "1995-03-01"

lubridate mdy format issue

I have a date column whose date values are something like this:
date = c("1/6/2022", "1/6/2022", "1/19/2022", "1/20/2022")
When I try to convert it to new date column using lubridate::mdy I get:
library(lubridate)
date_new = lubridate::mdy(date)
print(date_new)
[1] "2022-01-06" "2022-01-06" "2022-01-19" "2022-01-20"
# Desired output (mm-dd-yyyy or mm/dd/yyyy):
[1] "01-06-2022" "01-06-2022" "01-19-2022" "01-20-2022"
How can I get the desired output using lubridate?
We can use format
format(lubridate::mdy(date), '%m-%d-%Y')
[1] "01-06-2022" "01-06-2022" "01-19-2022" "01-20-2022"

How to reformat dates without NA's

I have a column of dates I need to reformat, they are in the format m/d/yyyy, but as the dates grow the format becomes mm/dd/yyyy. When I try to run
as.Date(x, format = '%m/%d/%y')
I get NA's
How can I reformat the dates to mm/dd/yyyy?
Here is an example:
# convert date info in format 'mm/dd/yyyy'
strDates <- c("01/05/1965", "08/16/1975")
strDates
Output:
[1] "01/05/1965" "08/16/1975"
Convert the dates using this code:
dates <- as.Date(strDates, "%m/%d/%Y")
Output:
[1] "1965-01-05" "1975-08-16"

How to convert a date to YYYYDDD?

I can't figure out how to turn Sys.Date() into a number in the format YYYYDDD. Where DDD is the day of the year, i.e. Jan 1 would be 2016001 Dec 31 would be 2016365
Date <- Sys.Date() ## The Variable Date is created as 2016-01-01
SomeFunction(Date) ## Returns 2016001
You can just use the format function as follows:
format(Date, '%Y%j')
which gives:
[1] "2016161" "2016162" "2016163"
If you want to format it in other ways, see ?strptime for all the possible options.
Alternatively, you could use the year and yday functions from the data.table or lubridate packages and paste them together with paste0:
library(data.table) # or: library(lubridate)
paste0(year(Date), yday(Date))
which will give you the same result.
The values that are returned by both options are of class character. Wrap the above solutions in as.numeric() to get real numbers.
Used data:
> Date <- Sys.Date() + 1:3
> Date
[1] "2016-06-09" "2016-06-10" "2016-06-11"
> class(Date)
[1] "Date"
Here's one option with lubridate:
library(lubridate)
x <- Sys.Date()
#[1] "2016-06-08"
paste0(year(x),yday(x))
#[1] "2016160"
This should work for creating a new column with the specified date format:
Date <- Sys.Date
df$Month_Yr <- format(as.Date(df$Date), "%Y%d")
But, especially when working with larger data sets, it is easier to do the following:
library(data.table)
setDT(df)[,NewDate := format(as.Date(Date), "%Y%d"
Hope this helps. May have to tinker if you only want one value and are not working with a data set.

Convert yyyymmdd string to Date class in R

I would like to convert these dates with format YYYYMMDD to a Date class.
dates <- data.frame(Date = c("20130707", "20130706", "20130705", "20130704"))
I tried:
dates <- as.Date(dates, "%Y%m%d")
And I get the following error:
Error in as.Date.default(dates, "%Y%m%d") :
do not know how to convert 'dates' to class "Date"
What would be the correct way to set this format?
You need to provide the Date column, not the entire data.frame.
R> as.Date(dates[["Date"]], "%Y%m%d")
[1] "2013-07-07" "2013-07-06" "2013-07-05" "2013-07-04"
An extra conversion to characters works for me:
dates<-as.Date(as.character(dates),format="%Y%m%d")
Without the conversion the following error occurs:
dates<-as.Date(dates,format="%Y%m%d")
Error in as.Date.numeric(dates, format = "%Y%m%d") :
'origin' must be supplied
Different error but this might help, works for POSIXct too, paste date and hours, format %Y%m%d%H
Classic R:
> start_numeric <- as.Date('20170215', format = '%Y%m%d');
> start_numeric
[1] "2017-02-15"
> format(start_numeric, "%Y%m%d")
[1] "20170215"
Use the lubridate package for an easy conversion:
date_test <- data.frame(Date = c("20130707", "20130706", "20130705", "20130704"))
date_test$Date <- ymd(date_test$Date)
date_test
Date
1 2013-07-07
2 2013-07-06
3 2013-07-05
4 2013-07-04
Instead of using brackets, you can use variable name:
dates <- data.frame(Date = c("20130707", "20130706", "20130705", "20130704"))
as.Date(dates$Date, "%Y%m%d")
[1] "2013-07-07" "2013-07-06" "2013-07-05" "2013-07-04"

Resources