Character date variable with various format outputs - "R" - 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
.

Related

How to Convert Multi date format into one in R

I am running some data pre-processing in Rstudio. There are two different kinds of date format in my data.frame. And I want to convert the numeric in the DOB column into %Y/%m/%d format like others. Could you please offer me some advice? Thanks in advance.
The nchar of that column will allow to distinguish the two different data formats. Then use the foramting to convert your date inputs into desired format.
This could be achieved using the tidyverse in one line
library(tidyverse)
my.data.frame%>%mutate(Date_new=ifelse(nchar(as.character(DOB))<5, format(as.Date(.$DOB, "%Y-%m-%d"), DOB)

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)

Convert Dates from a Data Frame from Numeric into Date Format in R (produces NAs)

I'm a Rookie with R. I have read in a Data Frame from Excel in R with the read.csv2 call, (Converted the Excel-file into csv).
I changed every Date in the table to a Y-M-D Format and wanted to use:
lapply(df$dates, as.Date, Format = "%Y/%m/%d")
but it produces NAs for every Date then.
When i ask for the mode it says the Dates are "numeric".
I tried to convert into character before into Dates with:
lapply(df$dates, as.character)
I dont know why it producs the NAs. Can someone help?
If you want to avoid the pain of finding the good format, there is dataPreparation package which provide a function to do that easily.
require(dataPreparation)
df <- setColAsDate(df, cols = "dates")
It will try to guess the format among thousand of various formats.
(NB: Please note that I'm the developer of this package.)

Specific kind of date conversion in 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")

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