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
  • 3,285
  • 4
  • 27
  • 57

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,612
  • 2
  • 49
  • 81
2

Have you tried ?

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