1

I have a dataframe with a "Date" column, several thousand rows long. The dates are within their own cell, in this format:

2006-06-24

How can I get my "Date" column down to just the year, and then apply it to the rest of the rows? The dates change, and go through 2018. Thanks.

papelr
  • 468
  • 1
  • 11
  • 42

3 Answers3

2

You can do this with sub and a regular expression.

Date = c("2006-06-24", "2007-07-25", "2008-08-26")
sub("(\\d{4})-.*", "\\1", Date)
[1] "2006" "2007" "2008"
G5W
  • 36,531
  • 10
  • 47
  • 80
2

If data is already in Date/POSIXct format then you can use format as:

# Sample data
v <- as.Date(c("2006-06-24","2006-06-25","2006-06-26"))

format(v,"%Y")
#[1] "2006" "2006" "2006"
MKR
  • 19,739
  • 4
  • 23
  • 33
1

Since you tagged dplyr, here's a tidyverse solution using lubridate::year():

library(lubridate)

dates <- c("2006-06-24", "2007-06-24")
year(dates)

[1] 2006 2007
andrew_reece
  • 20,390
  • 3
  • 33
  • 58