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))
Related
I have a character column of a df looking like:
a <- "6/10/21 19:34"
where the order is m/d/Y H:m.
I can't figure out why I am not able to convert it into datetime object.
I tried with:
mdy_hms(a,tz=Sys.timezone())
But I can't find a viable solution.
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 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)
Just a brief (and hopefully simple!) question;
I have a column of dates such as 08/14 and 08/15. However the column class is character, and I was wondering if it is possible (and if so how) to convert this into a date class? I've tried lots of different ways and it either does nothing, or returns N/A values for the whole column. Hopefully there should be a simple line of code to fix this that I haven't come across yet?
Any help much appreciated!!
One option is to create a day by pasteing and then use as.Date because date also needs day info
as.Date(paste0(c("08/14", "08/15"), "/01"), format = "%m/%y/%d")
#[1] "2014-08-01" "2015-08-01"
Or make use of as.yearmon from zoo
library(zoo)
as.Date(as.yearmon(c("08/14", "08/15"), "%m/%y"))
library(dplyr)
dates <- c("08/14", "10/13", "12/09") # year-month
datesWithYear <- paste(date, "/01", sep = "") %>%
as.Date()
If you want to use the year-month combination as a factor for cohort analysis of something similar you can always use as.yearmon() from the zoo package. tsibble and lubridate also have interesting functions for dealing with dates and time.
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")