Convert MM-DD-YYYY into MonthName-DD-YYYY [duplicate] - r

This question already has answers here:
Changing date format in R
(7 answers)
Closed 1 year ago.
How to convert 10-03-2021 into October-03-2021?
I have column in data frame in MM-DD-YYYY which needs to be converted into Month Name- Date- Year
example
10-03-2021 into October-03-2021

You have two parts: parsing the date, and then printing it with your format.
Parsing is quite easy with the lubridate package:
> library(lubridate)
> dates <- mdy(c('10-03-2021', '01-31-1995'))
> dates
[1] "2021-10-03" "1995-01-31"
For printing, you can then use the format() function:
> format(dates, '%B-%d-%Y')
[1] "October-03-2021" "January-31-1995"
%B is the full name of the month, %d the day and %Y the year (with all 4 digits)

Related

R Convert concatenated date in chr format to dd-mm-yyyy date format [duplicate]

This question already has answers here:
Mysterious error by parsing French dates on OSX
(4 answers)
Closed 11 months ago.
I've got a date in character format (month in French) that looks like this -
date = "30juillet2021"
I'd like to convert it to dd-mm-yyyy date format
Desired output
30/07/2021
I've tried a number of solutions on stackoverflow, but none seem to work.
Any help would be appreciated
Sys.setlocale('LC_TIME', "French_France")
[1] "French_France.1252"
as.Date(date, "%d%B%Y")
[1] "2021-07-30"
format(as.Date(date, "%d%B%Y"), "%d/%m/%Y")
[1] "30/07/2021"

Converting YY/MM/DD to MM/DD/YY in R [duplicate]

This question already has answers here:
How to convert date to format "yyyy-mm-dd" in R when input value can be of different formats
(3 answers)
Change Date print format from yyyy-mm-dd to dd-mm-yyyy
(2 answers)
Closed 2 years ago.
I need to find the difference in days between 2 date columns, but one of them is in the format of "6/16/2019" and the other is in the format of "2019-02-25". Not sure which one would be easier to convert to which, but would like to get end result in days. Which I know how to do. I would appreciate help converting the second yyyy-mm-dd to mm-dd-yyyy.
We can use functions from the lubridate package to convert the different formats to dates, and then subtract.
rawData <- "date1,date2
2002-05-15,6/16/2019
2019-12-31,4/15/2020"
data <- read.csv(text = rawData,stringsAsFactors = FALSE)
library(lubridate)
mdy(data$date2) - ymd(data$date1)
...and the output:
> mdy(data$date2) - ymd(data$date1)
Time differences in days
[1] 6241 106
>

Lubridate Date parsing is one year off [duplicate]

This question already has answers here:
date functions in R return wrong year
(2 answers)
Closed 3 years ago.
Within R, I'm trying to convert a text string into a Date variable type using lubridate's as.Date function.
I have a vector of values such as:
Dates
11/28/2019
11/29/2019
I am attempting to convert these to standard date variables using this as.Date function:
as.Date(Dates, "%m/%d/%y")
I do not receive an error message, and it correctly interprets the month and date, but for some reason it's outputting the wrong year - one year ahead:
"2020-11-28"
"2020-11-29"
I have no earthly idea why it is incorrectly interpreting the year in this way. Any help is appreciated!
We need to use %Y for 4 digit year as %y refers to only 2 digit
as.Date(Dates, "%m/%d/%Y")
Or using lubridate, this would be resolved
library(lubridate)
mdy(Dates)
Or with anydate from anytime
library(anytime)
anydate(Dates)

How to convert this timestamp format to standard timestamp format? [duplicate]

This question already has an answer here:
Dealing with timestamps in R
(1 answer)
Closed 6 years ago.
The timestamps I have as character class are in this format: 1/28/15 16:34 . How do I covert it to an R time stamp format and then also extract the hour of the day, day of the week and year separately as well?
You can use strptime function in this way:
my_time = strptime("1/28/15 16:34", "%m/%d/%y %H:%M")
Note in particular the %m and the %y to say, respectively, that months will be written with 1 character from Jan to Sept and year will be written with 2 character.
For example, if you need to convert "01/28/2015" you need %M and %Y:
my_time = strptime('01/28/2015 16:34', '%M/%d/%Y %H:%M')
To extract the day of week and the hour:
library(lubridate)
week_day = wday(my_time) # or wday(my_time, label=T) if you want the weekday label (Wed in this case)
day_hour = hour(my_time)

date format in R [duplicate]

This question already has answers here:
Add correct century to dates with year provided as "Year without century", %y
(3 answers)
Closed 5 years ago.
I have a date column in a data frame in chr format as follows:
chr [1:1944] "20-Sep-90" "24-Feb-05" "16-Aug-65" "19-Nov-56" "28-Nov-59" "19-Apr-86"
I want to convert to date using something like:
strptime(x=data$dob, '%d-%b-%y')
But I get several future dates in the result like
[1] "1990-09-20" "2005-02-24" "2065-08-16" "2056-11-19" "2059-11-28" "1986-04-19" "2041-04-01" "1971-01-23"
[9] "1995-11-25" "1995-11-25" "2009-02-11" "2002-09-19" "1977-10-06" "1998-03-22" "2050-03-12" "2030-03-26"
Is there a way to ensure I return dates that commenced in the correct century?
Thanks
It doesn't look (from the documentation for %y in ?strptime) like there's any obvious option for changing the default century inferred from 2-digit years.
Since the objects returned by strptime() have class POSIXlt, though, it's a pretty simple matter to subtract 100 years from any dates after today (or after any other cutoff date you'd like to use).
# Use strptime() to create object of class POSIXlt
dd <- c("20-Sep-90", "24-Feb-05", "16-Aug-65",
"19-Nov-56", "28-Nov-59", "19-Apr-86")
DD <- strptime(dd, '%d-%b-%y')
# Subtract 100 years from any date after today
DD$year <- ifelse(DD > Sys.time(), DD$year-100, DD$year)
DD
[1] "1990-09-20" "2005-02-24" "1965-08-16" "1956-11-19" "1959-11-28" "1986-04-19"
dd <- c("20-Sep-90", "24-Feb-05", "16-Aug-65",
"19-Nov-56", "28-Nov-59", "19-Apr-86")
library(lubridate)
DD=dmy(dd)
https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html http://vita.had.co.nz/papers/lubridate.pdf
strptime(data$dob, "%Y/%m/%d")

Resources