Convert date in YYYY-MM-DD format to YYYY-MM - r

I would like do perform the reverse of R converting a factor YYYY-MM to a date.
I have a data frame (df) with dates presented as YYYY-MM-DD format (column "Date1") and would like to convert them into YYYY-MM format. The days range between 0-31 but these aren't necessary for my YYYY-MM format.
The point is that I have another set of columns that are in YYYY-MM format (column "Date2") and want to calculate the differences between them in whole months (values x, y and z in column "Difference(months)")
Date1 Date2 Difference(months)
2008-08-11 1995-02 x
2010-06-18 1972-09 y
2011-04-22 1956-11 z
Doing df$Date1_new <- as.Date(df$Date1, "%Y-%m") just gives a series of "NA" values.
Please can you give me a way that does what I am asking for or another easier method that gives the same desired result?

Using format(as.Date(df$Date1, "%Y-%m-%d"), "%Y-%m") works for converting 2008-08-11 to 2008-08 - as suggested by #docendo discimus

Related

Converting characters formats to dates returns NAs

I'm looking to convert the Dates which are currently in chr format
Date
July2021
August2021
to
Date
2021-07
2021-08
when I run the code below my Date column are all NAs
df$Date <- as.Date(df$Date, "%B%Y")
This is for your reference. Add 01 so that it becomes a date
sampleD= "August2021"
substr(as.Date( paste0('01',sampleD), "%d%B%Y"),1,7)
Result:
'2021-08'

How to calculate time difference in R using an dataframe

Have an large data frame where there's 2 columns (POSIXct) and need to calculate length of ride.
Dates are formatted as follows:
format: "2020-10-31 19:39:43"
Can use the difftime function, correct?
Thanks
Given your data is using the correct POSIXct format you can simply subtract two dates to get the difference. No need for additional functions.
date1 <- as.POSIXct(strptime("2020-10-31 19:39:43", format = "%Y-%m-%d %H:%M:%OS"))
date2 <- as.POSIXct(strptime("2020-10-31 19:20:43", format = "%Y-%m-%d %H:%M:%OS"))
date1 - date2
Output: Time difference of 19 mins
It depends what output format you want.
For example if you want month difference between two dates, you can use the "interval" function from library "lubridate"
library(lubridate)
interval(as.Date(df$date1),as.Date(df$date2) %/% months(1))
It also works with years, weeks, days, hours

Converting yyyy numeric data to date format in R

I have a dataset, df with a column containing dates in yyyy format (ex: 2018). I’m trying to make a time series graph, and therefore need to convert them to a date format.
I initially tried, df$year <- as.Date(df$year) but was told I needed to specify an origin.
I then tried to convert to a character, then a date format:
df$year <- as.character(df$year)
df$year <- as.Date(df$year, format = “%Y”)
This seems to have worked, however when it changed the all the years to yyyy-mm-dd format, and set the month and day to April 5th, today. For example 2018 becomes 2018-04-05.
Does anyone have an idea for how to fix this? I would like it to start on January 1, not the day I am performing the conversion. I also tried strptime(as.character(beer_states$year), “%Y”) with the same result.
Any help would be very much appreciated. Thanks!
Add an arbitrary date and month before converting to date.
df$Date <- as.Date(paste(df$year, 1, 1), '%Y %m %d')
We can use as.yearmon
library(zoo)
df$Date <- as.Date(as.yearmon(df$year, '-01'), '%Y-%m'))

How to format dates in R

I'm having trouble changing date format in R. I have a vector "StartDate" with dates and time for instance in the format:
01Feb1991 00:00
I did:
as.POSIXct(as.character(bio$StartDate), format = "%d/%m/%Y %H:%M")
...but I got NAs as a result. Would there be a different way to change the vector into date format?
The format you provide has to match your string. In your case, that's '%d%b%Y %H:%M' (you don't have slashes between day, month and year, and your month is the abbreviated name, not the number).
as.POSIXct('01Feb1991 00:00', format='%d%b%Y %H:%M')
See ?strptime (mentioned in ?as.POSIXct) for various tokens you can use for dates.

Convert from character to Date

I am running into some date issues when working with Dates in R.
Here's my situation-
I have a data set based on dates and finally got the Date field converted from character to Date in R using the following code
o1$Date <- as.Date(o1$Date , "%m/%d/%y")
(My dataset is o1 and Date is the name of my Date column)
My Date column has the following values
"1/1/2013" "1/1/2014" "1/10/2013" "1/10/2014" "1/11/2013" "1/11/2014"
However when I convert the Char to Date I get the following Dates
"2020-01-01" "2020-01-01" "2020-01-10" "2020-01-10" "2020-01-11"
Any suggestions on what the problem could be and how to work around it?
look at ?strptime to see the formatting options for times and dates. You need to use %Y rather than %y which is for a 2 digit year.

Resources