Change dates to first day of the month in R [duplicate] - r

This question already has answers here:
Changing date format in R
(7 answers)
How to round floor_date() to arbitrary date?
(1 answer)
Change day of the month in a Date to first day (01)
(5 answers)
Closed 3 years ago.
How do I change my dates to the first day of the month...so 3/31/19 becomes 3/1/19, 2/28/19 becomes 2/1/19, and so on.
Thanks

One of the many ways to do it is to use lubridate package.
library(lubridate)
date <- dmy('11/02/2019')
day(date) <- 1
date

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")

How to find time interval between two dates that are in year and week format eg. 201630 and 201951 in r [duplicate]

This question already has answers here:
How to Parse Year + Week Number in R?
(3 answers)
Get the difference between dates in terms of weeks, months, quarters, and years
(9 answers)
Closed 3 years ago.
How to find the interval between two dates that are in year and week format. Example 201630 and 201851
As #thelatemail pointed out if you have week number you also need day of the week to get the date. We can use %U to identify week number and %u to get day of the week. We use an arbitrary day of the week (here 1).
d1 <- as.character(201630)
d2 <- as.character(201851)
diff(as.Date(paste0(c(d1,d2),"1"), format="%Y%U%u"))
#Time difference of 882 days
Of if you need output in another unit we can also use difftime
difftime(as.Date(paste0(d2, 1), "%Y%U%u"),
as.Date(paste0(d1, 1), "%Y%U%u"), units = "weeks")

`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).

Get the year from a timestamp in R [duplicate]

This question already has answers here:
Extract month and year from a zoo::yearmon object
(7 answers)
Closed 7 years ago.
I have an r dataset which has a list of timestamps like this: 2009-08-18 14:38:20 2010-08-10 14:58:25 etc
I want to extract the year but the Posixct doesn't have a years function unlike the months(t$timestamp)
Is there a way to get only 2009?
Use format:
x <- as.POSIXct(c("2009-08-18 14:38:20", "2010-08-10 14:58:25"))
format(x, "%Y")
Have you tried ?
strptime(yourtimestamp, "%Y")

Resources