Make date readable by R - r

I have a column in a dataset right now formatted for example as "Aug-19" which represents 08/01/2019 in mm/dd/yyyy format. How do I convert this into a format that can be read by R?

You can use lubridate package.
dates <- c("Aug-19", "Sep-19")
dates_myd <- paste0(dates, "-01")
lubridate::myd(dates_myd)

With lubridate we can also do
myd(dates, truncated = 1)
#[1] "2019-08-01" "2019-09-01"
data
dates <- c("Aug-19", "Sep-19")

Related

How to fix timestamp in character class to correct format in R?

I am having trouble converting timestamps in character class. I am trying to use following function
df$Time <- strptime(df$Time, format = "%H:%M:%S")
Then problem I have is that my Time column (currently class Character) has values listed like this 1:00:05 or 55:42. There are no dates in the column.
While the 55:45 is in mm:ss, the 1:00:05 is in hh:mm:ss. Could this be the problem? I can't think of anything else. When I run the above function, I keep getting NA for the whole column. How can I resolve this.
Thanks for any help. I have been stuck and have tried so many different functions such as hms from lubridate and as.POSIXct
Here is sample dataset
df <- data.frame(Time = c("1:00:05", "55:45","34:33","1:12:35"))
df$Time <- as.character(df$Time)
head(df)
We may use parse_date_time
library(lubridate)
parse_date_time(df$Time, c("HMS", "MS"))

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"

R convert string "Jan\n1990" to date format

I have a csv file with the first column (named "month") having month data in the format
"Jan\n1990", "Feb\n1990" and so on.
When I read the file in R using read.csv function (with stringsAsFactors = FALSE) it reads the "month" column as 'chr'. I want to convert it into date format. I tried
month_1 <- as.Date(f4$month)
But it gives the error
Error in charToDate(x) :
character string is not in a standard unambiguous format
How do I convert the first column into a date formatted column?
In r, using the function parse_date_time from lubridate package, you can convert your character string into a date format:
date <- c("Jan\n1990","Feb\n1990") # Example of character strings to convert into dates
library(lubridate)
parse_date_time(date, order = "bY")
[1] "1990-01-01 UTC" "1990-02-01 UTC"
A solution based on as.Date:
date <- c("Jan\n1990","Feb\n1990")
as.Date(paste0("1",date), format="%d%b\n%Y")
# [1] "1990-01-01" "1990-02-01"
Another option is as.yearmon from zoo
library(zoo)
as.Date(as.yearmon(date))
#[1] "1990-01-01" "1990-02-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")

Change factor to a date format in R

I have a file with dates in the format YYYY-mm. I am trying to prepare my data for a time series analysis and therefore need to convert the formats from factor to a Date format.
What I've tried:
x <- '2011-11'
as.Date(as.character(a), "%Y-%m")
the last line gives me an output NA.
Thank you!
"2011-11" is not a date (where's the day)? Either add a day:
a <- '2011-11'
as.Date(paste0(a,'-01'))
or use zoo's yearmon class:
library(zoo)
as.yearmon(a)
'2011-11' isn't a date. Use paste(your.year.month.strs, '01', sep='-') to add the day component to your strings, then call as.Date with "%Y-%m-%d".

Resources