Warm tip: This article is reproduced from stackoverflow.com, please click
r

Get the year from a timestamp in R

发布于 2020-03-27 15:40:39

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?

Questioner
Morpheus
Viewed
30
Backlin 2015-05-07 19:19

Use format:

x <- as.POSIXct(c("2009-08-18 14:38:20", "2010-08-10 14:58:25"))
format(x, "%Y")