Specific kind of date conversion in R - r

I have looked through the site and cannot find a way to convert a date into a specific format that I need in order to properly read file names. Any help would be appreciated. I am trying to get from the standard date format
2013-06-01
to the date format of
06012013
My current attempt is
date <- as.Date("2013-06-01")
newdate <- paste(month(date),day(date),year(date))
But that leaves me with a date of
612013
How can I make sure that I keep the 0's in the new date format?

We can use format
format(date, "%m%d%Y")
#[1] "06012013"
where
date <- as.Date("2013-06-01")

Related

How to change date format in R from 01-Oct-21 to 02-10-2021

I have my date column formats in 01-Oct-21 and I want to change them to 01-10-2021. Does anyone knows how to do this in R?
Thanks!
Using base R. You might need to convert your original column into Date format using
old_date <- as.Date('01-Oct-21', format = '%d-%b-%y')
Then you use the format function to get into what you what
format(old_date, '%d-%m-%Y')
It will look slightly different if your dates are in a data frame.
We can use
library(lubridate)
format(dmy("01-Oct-21"), '%d-%m-%Y')
[1] "01-10-2021"

How to sort 'mm-yyyy' format in R?

Date
01-2018
02-2018
01-2019
02-2019
I tried using arrange(df, Date)
It gets arranged as
01-2018
01-2019
02-2018
02-2019
Here is one base R option. We can try ordering the data frame using an on the fly date based on the text strings.
df <- data.frame(Date=c("01-2018", "02-2018", "01-2019", "02-2019"),
stringsAsFactors=FALSE)
df[order(as.Date(paste0("01-", df$Date), format="%d-%m-%Y")), ]
[1] "01-2018" "02-2018" "01-2019" "02-2019"
Note that I form a complete date by arbitrarily using the first of the month, for each text date, using as.Date with the correct format mask to generate a bona fide date.
For best results, consider storing your dates in a proper date column, or, if you must use text, use an ISO format which would at least sort properly.

I can't convert an unknown column in to a date column and then change format to dd-mm-yyyy

I know it seems that this may be a repeated question, but I have tried other solutions and still cannot get it to work. I have uploaded a .csv file into r. I have done a small amount of house cleaning but ultimately I would like to convert a column from '"POSIXct" "POSIXt"' to a 'date' column type, and a 'character' column to a 'numeric'. For the latter column (change) I have decimals and --- entries, I converted the --- to NA, but fail to convert it to a 'numeric' afterwards.
df$value <- as.numeric(as.character(df$value))
I first used:
df$date <- dmy_hm(df$time_stamp, tz = "Europe/London")
to create a new date variable / column. But this did not give 'date' as a column type. I then tried using:
df$date <- as.Date(df$date)
but this did not work. Once I have converted to 'date' I need to convert the format from yyyy-mm-dd hh:mm:ss to dd/mm/yyyy.
Any help with will greatly received.
lubridate package can be wacky sometimes. Can you share head of you .csv data? you might have confused with dmy_hms with myd_hms or ymd_hms formats. Try using anytime package.
anytime::anytime(df$time_stamp)

Character date variable with various format outputs - "R"

I am using "R" and am hoping someone can assist with my date formatting issue. I have a character variable from a dataset that I Imported from Excel.
DateVar <- c("12-07-2017", "43229", "43137", "03-27-2018")
The excel file I am using has two date formats in the same variable (MM-DD-YYYY and YYYY-MM-DD), hence the two formats in "DateVar". The date formatted YYYY-MM-DD converts to the excel date (i.e 43229).
I would like to have all the values be the same date format (ideally YYYY-MM-DD), but I am having issues converting them consistently.
Your help is much appreciated.
You can create an indicator vector for the observations that have been converted wrongly:
indicator <- !grepl("-", DateVar)
Then you can use this vector to convert these dates using the answer from this - How to convert Excel date format to proper date with Lubridate
.

Convert non-standard date format to date in R

I have dates in the format 01jan2000 (without a space or any separator) and need to convert this to a date in R so I can calculate ages. I have tried both
mydata$censor_date <- as.Date(mydata$censor_date, "%d-%b-%Y")
and
mydata$censor_date <- as.Date(mydata$censor_date, "%d-%m-%Y")
But I only get NAs. I can do this in Excel, but would prefer to have one script to run rather than switch between programmes. The exact format of the date isn't important, as long as I can use R to calculate ages.
Thanks in Advance
If you have date in "01jan2000" format you not need "-" in format.
Try
as.Date(mydata$censor_date, "%d%b%Y")

Resources