3

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?

Morpheus
  • 2,985
  • 3
  • 25
  • 51

2 Answers2

6

Use format:

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

Have you tried ?

strptime(yourtimestamp, "%Y")
Sacha
  • 53
  • 7