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)
Related
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"
I have a df with a column of the formate df$date = (1800.01, 1800.02, 1800.03) and so on.
And I can't figure out how to convert these numbers into proper monthly dates.
I tried the function date_decimal from the lubridate package, but that does not work for how my dates are formated.
Any ideas?
Probably the best way is to convert to a character first, then use one of the standard conversion functions. e.g.:
lubridate::ym(as.character(df$date))
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 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
.
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.)