I need to import dates using read.csv. The date are in "dd-mm-yyyy" format in csv file. I've appended sample data below.
UniqueId DOB
- 01-04-1984
- 24-08-1904
- 12-12-2006
- 05-05-1870
Read.csv is converting the date into "dd-mm-yy" format even when I'm importing date as character. I need it to import all 4-digit year.
My code and results are:
x <- read.csv("file", header=TRUE,colClasses =c("DOB"="character"))
I also tried:
x <- read.csv("file", header=TRUE, stringsAsFactors = FALSE)
Result from both:
UniqueId DOB
- 01-04-84
- 24-08-04
- 12-12-06
- 05-08-70
> class(x$DOB) [1] "character"
When I use as.Date function on this, I get error values:
> as.Date(dob$DOB, format="%d-%m-%y")
[1] "01-04-1984" "24-08-2004" "12-12-2006" "05-08-1970"
I read that as.Date function automatically turns years between 00 and 68 into 21st Century years and years between 69 and 99 into 20th Century years.
Thus, I think I'm making a mistake in read.csv function itself.
%y
is for 2-digit years,%Y
is 4-digit years. See?strptime
for details. – Gregor Thomas