1

I am working a dataset which has a Date column currently stored as Character, below is the format of how the dates are:

31/12/00

01/01/01

I want to change the year part from "00" to "2000" and "01" to "2001" for the whole column and want it in date format.

How can I do it?

enter image description here

I tried doing the following way

if(y2k_clean$Date[7:8] == "00")
  replace(y2k_clean$Date, 7, "2000")

I also tried to achieve this with gsub() but could not get the result I wanted.

Thank you.

  • 1
    How do we know that `00` corresponds to `2000` and not `1900` ? – Tim Biegeleisen Dec 16 '22 at 13:28
  • 3
    Please don't post your data as images and instead post them as code (ie, using `dput()`) – jpsmith Dec 16 '22 at 13:32
  • 1
    Hi Nihar Patel. Welcome to SO! Please add a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) instead of posting pictures. That said, you could do: `y2k_clean$new_Date <- gsub("(\\d{2})$", "20\\1", y2k_clean$Date)`, but I expect that you'd be better off by converting to a `date`-type soon :-) – harre Dec 16 '22 at 13:32
  • 2
    Do you want it as date formatted or character formatted? – jpsmith Dec 16 '22 at 13:33
  • @TimBiegeleisen, Hello Tim I know the data set is about the year 2000-2001 so I want it converted to those values. – Nihar Patel Dec 16 '22 at 13:50
  • 1
    @jpsmith Sure mate, will do that from now on. I want is in Date format. Thanks. – Nihar Patel Dec 16 '22 at 13:51
  • @harre Hello, will have a look at how add a reproducible code. Thanks for the info and also for the code and guidance. – Nihar Patel Dec 16 '22 at 13:53
  • 1
    NiharPatel, you say you want it in "Date format", which means you cannot display it in R as `%d/%m/%Y`. R displays dates always and only as `%Y-%m-%d`. If you want to render it in a report as `%d/%m/%Y`, that's fine, but I suggest you keep it `Date`-class (Ymd) as long as possible and convert to dmY only when rendering the table/plot/report. There are ways around this, but they are incomplete, rife with corner-cases, and generally too much work and risk. – r2evans Dec 16 '22 at 14:21

2 Answers2

2

You could first convert it to a date with the format and after that convert it to your format with %Y as 4 digit year like this:

dates <- c('31/12/00', '01/01/01')
format(as.Date(dates,format='%d/%m/%y'), "%d/%m/%Y")
#> [1] "31/12/2000" "01/01/2001"

Created on 2022-12-16 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • Hello Quinten, that worked for me they are now in format I want. Can you explain the part after (format = '%d/%m/%y') this is used for current format? and then we convert the year with "%Y". How did it take "00" as 2000? Thank you. – Nihar Patel Dec 16 '22 at 14:04
  • Hi @NiharPatel, %y is year with 2 digits like 01 and %Y is year with 4 digits like 2001. – Quinten Dec 16 '22 at 14:05
0

You can use format:

df <- tibble(Date = c("31/12/00", "01/01/01"))
format(as.Date(df$Date, format="%d/%m/%y"))
juanbarq
  • 374
  • 6