Converting a suffix based date in R - r

I'd like to convert the following date format: Wed 1st Jan 2014 to 01/01/2014
The suffix following the 1 is giving me the most trouble and preventing me from using the standard methods.

One option would be dmy from lubridate to convert to Date class and then format it to required format
library(lubridate)
format(dmy(str1), "%d/%m/%Y")
#[1] "01/01/2014"
data
str1 <- "Wed 1st Jan 2014"

Related

Converting Date Formats in R - Strange format

I have an date column with dates like this
Mon Jun 29 13:28:37 BRT 2020
I would like to convert it to something like 29/06/2020.
How can i do this using R?
Maybe this helps:
date<-Sys.time()
format(date, "%d/%m/%Y")
The output of the call from "format" should look like:
"16/04/2021"
With uppercase lettres of the formatstring format the date into the long version of the year.
format(date, "%d/%m/%y") would result into:
"16/04/21"

How to convert full date character into mm/dd/yyyy in R?

I have a dataset in R with a date column that is currently being treated as a character column. The dates in this column are listed as "August 24 2012" and I am trying to convert it into a date format such as 08/24/2012.
I am a novice with R and have tried to use format() and lubridate with no success. How do I convert these dates from a character to date?
We need to convert it to Date class and then use format
format(as.Date(dates, "%B %d %Y"), "%m/%d/%Y")
#[1] "08/24/2012"
As the order is month, day, year use mdy from lubridate
library(lubridate)
format(mdy(dates), "%m/%d/%Y")
#[1] "08/24/2012"
It is based on the order, e.g.
ydm("2012 24 August")
#[1] "2012-08-24"
data
dates <- "August 24 2012"

Subtract months from time in format year-month

I want to subtract months from given date in year and month format.
global_date = "2017-01"
I am converting it with the zoo library as follows:
as.yearmon(global_date) - 0.1
but it gives me Nov 2016, I want it as '201612'
How can I do it in R?
As we want to subtract one month, we should subtract 1/12 which is 0.083 and not 0.1
library(zoo)
as.yearmon(global_date) - (1/12)
#[1] "Dec 2016"
If we need output in the mentioned format
format(as.yearmon(global_date) - (1/12), "%Y%m")
#[1] "201612"
Using only base R and making only minimal changes to the OP's code
format(as.Date(paste0(global_date, "-01")) - 0.1*10, "%Y%m")
#[1] "201612"
NOTE: No external packages used

how to convert time stamp 07 Mar 2016 01:00:03 PM to numeric in r

Im trying to find time difference between two timestamps, say, start and endtime. But the stamps are in string format :07 Mar 2016 01:00:03 PM. Can someone help how to convert this in numeric and find the time difference? Thanks
lubridate is your new best friend so download it right away to get to know her.
install.packages('lubridate')
library(lubridate)
Based on your example, use dmy_hms()
a <- dmy_hms('07 Mar 2016 01:00:03 PM')
# "2016-03-07 13:00:03 UTC"
b <- dmy_hms('08 Mar 2016 01:00:03 PM')
# "2016-03-08 13:00:03 UTC"
Then a simple subtraction could get the date diff.
b - a
# Time difference of 1 days
Or you can also use the as.numeric() use this number for calculation later.
as.numeric(b - a)
# 1

Unable to convert Month-Year string to Date in R

I'm using as.Date to convert a string like Aug-2002 to a dates object representing just the month of Aug of 2002, or if a day must be specified, Aug 1, 2002.
However
> as.Date(c('07-2002'), "%M-%Y")
[1] "2002-11-06"
> as.Date(c('Aug-2002'), "%b-%Y")
[1] NA
Why does the first line of code convert it to a different month and day? And the second one is NA?
I referred to this table for the formatting symbols.
The problem you are having is that the dates you have do not have a day value. Without the day value the format="%m-%Y" will not work in as.Date. These options below will solve them:
as.Date(paste0('01-', c('07-2002')), format="%d-%m-%Y")
library(zoo) #this is a little more forgiving:
as.yearmon(c('07-2002'), "%m-%Y")
as.yearmon(c('Aug-2002'), "%b-%Y")
as.Date(as.yearmon(c('07-2002'), "%m-%Y"))

Resources