1

It is what my row data looks like:

            Extraction   BORN
1             30/06/06  31/01/48
2             30/06/06  20/05/74
3             30/06/06  20/02/49
4             30/06/06  06/07/53
5             30/06/06  26/05/63
6             30/06/06  20/05/74

I want to use as.Date function to convert the date format. For example,I want to change 30/06/06 into 2006-06-30, and 31/01/48 change into 1948/01/31 so my code is:

data$Extraction<-as.Date(data$Extraction, "%d/%m/%y")

data$BORN<-as.Date(data$BORN, "%d/%m/%y")

But they all convert into NA as result. Dose anyone know how to solve this problem?

Amber Tseng
  • 65
  • 2
  • 8
  • 2
    `as.Date("30/06/06", "%d/%m/%y")` works for me. What do you get with `str`? – sequoia Aug 04 '18 at 08:42
  • @sequoia It is factor format. – Amber Tseng Aug 04 '18 at 09:02
  • `as.Date(factor(c("30/06/06","31/01/48")), format="%d/%m/%y")` works too (though you get 2048 not 1948 because of R's year formatting rules). – thelatemail Aug 04 '18 at 09:18
  • `?strptime` gives all the rules for 2-digit year formatting - "%y - Year without century (00-99). On input, values 00 to 68 are prefixed by 20 and 69 to 99 by 19 - that is the behaviour specified by the 2004 and 2008 POSIX standards" – thelatemail Aug 04 '18 at 09:44

1 Answers1

1

Since the variables are factors, this should work:

data$Extraction<-as.Date(as.character(data$Extraction), "%d/%m/%y")

data$BORN<-as.Date(as.character(data$BORN), "%d/%m/%y")

EDIT:

I tried it out but your code should work on factors as well.

> x <- data.frame(date = as.factor("30/06/06"))
> x
      date
1 30/06/06
> as.Date(x$date, "%d/%m/%y")
[1] "2006-06-30"
> as.Date(as.character(x$date), "%d/%m/%y")
[1] "2006-06-30"
Stan
  • 480
  • 1
  • 5
  • 18
  • Thanks, it works! But I encounter a new problem is that some "future date" appear , like 2048/01/31, which actually should be like 1948/01/31 – Amber Tseng Aug 04 '18 at 09:22
  • @PeiChiTseng you could fix that with `as.Date("30/06/48", "%d/%m/%y") - years(100)` from the `lubridate` package. – Stan Aug 04 '18 at 09:28
  • @PeiChiTseng to find whether each date is a "future date" you can use something simple like `as.Date("30/06/48", "%d/%m/%y") > Sys.Date()` – Stan Aug 04 '18 at 09:33
  • @PeiChiTseng Have not tried it out but something like this might work: `data[data$Extraction > Sys.Date()]$Extraction <- data$Extraction - years(100)`. – Stan Aug 04 '18 at 09:37
  • Thank you very much! It is really helpful solution. – Amber Tseng Aug 04 '18 at 09:43