Get the year from a timestamp in R [duplicate] - r

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

Related

How To Change the Date format in a df [duplicate]

This question already has answers here:
Changing date format in R
(7 answers)
Closed 2 years ago.
I have this date format: 2016-04-14 and I want to change it to 14/4/16
This seems like a simple task but for some reason my strategy is not working. Here is my code:
''''
df$Date <- as.Date(df$Date,format = "%d/%m/%y")
''''
If you want date in specific format use format :
format(as.Date(df$Date), "%d/%m/%y")

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

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

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

Convert integer YYYYWW to date [duplicate]

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

Converting dates with a format of d/mm/yy (for years earlier than 69) [duplicate]

This question already has answers here:
Add correct century to dates with year provided as "Year without century", %y
(3 answers)
Closed 9 years ago.
Converting the character "6/07/69" into a date using the as.Date function results in "2068-07-06" instead of "1968-07-06". How can I fix this?
Example:
as.Date(c("6/07/68", "6/07/69"), format="%d/%m/%y")
[1] "2068-07-06" "1969-07-06"
you can use library chron
e.g.
> library(chron)
> as.Date(chron(c("6/07/67", "6/07/69"), format = c(dates = "m/d/y")))
#[1] "1967-06-07" "1969-06-07"

Resources