Convert integer YYYYWW to date [duplicate] - r

This question already has answers here:
Transform year/week to date object
(2 answers)
Closed 4 years ago.
The variable I want to convert is an integer in the form of YYYYWW.
So, for example 200901 represents week 1 of 2009, 201223 --> 23 of 2012 and so on.
I want to convert this variable into date format based on weeks in a year.
So in my example 01-2009 and 23-2012 or a similar format. I already tried several lubridate and ISOweek functions but never come up with a good result.
I really appreciate your help.

Not sure if it works for weeks but try the solution here:
https://stackoverflow.com/a/29928301/5335354
Something like:
df <- transform(df, x = as.Date(as.character(x), "%Y%U"))

Related

How to reformat dates to dd/mm/yyyy to yyyy [duplicate]

This question already has answers here:
Extract year from date
(7 answers)
Closed 2 years ago.
Essentially, I just need to drop the extra info provided by the day and month, and only retain the year.
My data is in the form of
dd/mm/yyyy (so, 20/11/2001 for example) and I want to just retain the yyyy (so 2001).
This question and answer: R: How to remove the day from a date?
have been somewhat helpful, but I have thousands of individual dates and so copying in the exact dates I need to change isn't going to work.
Does anyone have any idea how I can do this??
Convert to date class, then extract year using format:
format(as.Date(df1$Date, format="%d/%m/%Y"),"%Y")

R is giving the wrong max date [duplicate]

This question already has answers here:
as.Date with dates in format m/d/y in R
(4 answers)
Closed 2 years ago.
I am having an annoying issue in R that is not recognizing the correct date in my dataframe as the max date. I am creating a weighted average based on subtracting the date in the dataframe from the max date, so this is an annoying issue to deal with. I have tried reformatting all of the dates, this did not work.
Anything that I can try? The dates range from 1/1/2020 to 12/23/2020. But when I use max(PlayerData$Date) I am getting 3/9/2020.
Thanks
The issue is that max() is not working for characters. You need to transform it to date class. Here an example:
#Data
PlayerData <- data.frame(Date=c('3/9/2020','1/1/2020','12/23/2020'),stringsAsFactors = F)
Your approach:
#OP
max(PlayerData$Date)
Output:
max(PlayerData$Date)
[1] "3/9/2020"
Setting as date:
#Date
max(as.Date(PlayerData$Date,'%m/%d/%Y'))
Output:
max(as.Date(PlayerData$Date,'%m/%d/%Y'))
[1] "2020-12-23"

`as.Date()` identifies years in short form(say 01/10/68) as `2068-10-01` instead of `1968-10-01`, how to fix it? [duplicate]

This question already has answers here:
R as.Date conversion century error
(1 answer)
as.Date with two-digit years
(3 answers)
Is there a more elegant way to convert two-digit years to four-digit years with lubridate?
(3 answers)
Closed 3 years ago.
I have a csv dataset that has date of births stored as characters in the format - 01-06-68("%d-%m-%y"). I tried to convert the dates to Date objects using as.Date() but it identifies the year as 2068 when in fact the year is 1968. I understand Date starts from 1970-01-01 in R, is there an easy fix to this?
I am using the lubridate package to find the age.
Some age were negative due to the wrongly identified year.
elapsed <- train$Date.of.Birth %--% Sys.Date()
train$age <- floor(as.duration(elapsed) / dyears(1))
Maybe try
format(as.Date(date,format="%d-%m-%y"), "19%y-%m-%d")
You probably need to use %Y anyway bite the bullet and create a new df with complete years else your are screwed when 19xx transitions to 20xx.
Good Luck.

Why does R impute the 12th of the month when formatting a year as a Date [duplicate]

This question already has answers here:
Convert four digit year values to class Date
(5 answers)
Closed 5 years ago.
I note in R if one calls as.Date(as.character(2002:2013), format='%Y') the output is
[1] "2002-01-12" "2003-01-12" "2004-01-12" ...
I would like R to give me the first of the month instead. I could supply the whole date, paste(2002, '01', '01', sep='-'), but am curious why the year-only format imputes the 12th of the month and also to see other solutions.
Ah, just found my answer: The missing sections of the Date object (month/day) are imputed from today's date (System Date).

as.Date keeps on returning NAs [duplicate]

This question already has answers here:
How to convert a character string date to date class if day value is missing
(1 answer)
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 6 years ago.
So I've been through some Stack Exchange answers, and I can't resolve this.
I have a column in a dataframe that has dates as characters as follows
2011-12
2012-04
2011-10
etc
I would like to convert these to date formats which I have tried to do as follows:
Tots$DatesMerge<-as.Date(Tots$DatesMerge,"%Y-%m")
but I get NA's back all the time.
I tried to do as here but no joy. I'm really not sure what I'm doing wrong.
I'd say as.Date won't be able to work on values where there's no day of the month. You could try with zoo, as long as you don't mind it coming out as a yearmon class:
library( zoo )
as.yearmon( Tots$DatesMerge )
Alternatively, you can specify a day of the month to use as a dummy:
as.Date( paste0( Tots$DatesMerge, "-15" ) )
Edit: there is already an answer and it is a duplicate, but I suppose the explanation can be useful for further readers, so I'll leave it.
Explanation
This comes from the documentation in R, "Dates are represented as the number of days since 1970-01-01, with negative values for earlier dates".
In R, dates are thus dependent on year, month and days or at least an integer that represent the span (in days) from or to 1970-01-01. As such, the base Dates package in R cannot convert the data formated in years and month into dates since there are no days.
Solution
As a consequence, you have the option, if you go with the base R package, to provide a a day that would be used to convert your data.
Tots$DatesMerge <- as.Date(paste0(Tots$DatesMerge,"01"),"%Y-%m-%d")

Resources