0

I have a column of datetime with values like 10/10/49 20:30, but need to mutate the dataframe to have a column of just the date in format 1949. The code below is just bringing me a column of NA. How can I extract the year? I need to use dplyr to solve this!

df %>%
  mutate(., year = format(as.Date(x = datetime, format="%d/%m/%y %I:%M:%S %p"),"%Y"))
Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • 1
    Does this answer your question? [Extract year from date](https://stackoverflow.com/questions/36568070/extract-year-from-date) – mnm Jun 01 '20 at 23:26
  • I saw it, but I'm not sure how to do it through dplyr in the format I have it in. – user13451888 Jun 01 '20 at 23:28
  • then its suggested to revise your existing Q such that you've clearly stated how the Q is different from previously asked Q's. What all you've tried to solve the problem. What has worked and what did not work. Furthermore, see this post and learn how to create a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – mnm Jun 01 '20 at 23:35

2 Answers2

0

2 digit years are confusing. Looking at the date "10/10/49" how do you know whether it is 1949 or 2049?

R prefixes 2 digit year from 00-68 with 20 (2000-2068) and 69-99 with 19 (1969-1999) so based on your data you need to come up with a condition that will adjust the year based on your requirement.

For example, let's say you know that you don't have any dates greater than the current year in your data in that case you can do :

library(dplyr)
library(lubridate)

df %>%
  mutate(datetime = dmy_hm(datetime), 
         year = year(datetime), 
         rev_year = if_else(year > 2020, year - 100, year))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

We can use base R to do this

df$datetime <- as.POSIXct(df$datetime, format = "%d/%m/%y %I:%M:%S %p")
df$year <- as.integer(format(df$datetime, "%Y"))
df$rev_year <- with(df, ifelse(year > 2020, year - 100, year))
akrun
  • 874,273
  • 37
  • 540
  • 662