Change full column of dates in R - r

I have a full column of dates that are formatted like dd/mm/yyyy
I want the change the full column so that they are formatted like yyyy-mm-dd

Use dmy from lubridate
library(lubridate)
dmy("15/10/21")
[1] "2021-10-15"

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"

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)

Conversion of numeric vector to date

I have a dataframe with a date column like the following:
19870703
19920929
20051015
20030613
How can I change the numeric Data to a Date format like %Y%m%d?
Neither as.date() nor as.POSIX.ct() works?
Using lubridate
date <- ymd("19870703")
in place of date you can put your column name
as.Date("19870703", "%Y%m%d")
[1] "1987-07-03"
Apply for entire column.
library(date)
as.date(your.date.vector)

split dates column in R when there are no sperators

I have a data set with date column with following format i.e. 19700101
How can I convert it as 1970-01-01 format
I tried zoo package in R but could not work it out. Can any one help me on this
library(anytime)
anydate(19700101)

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