Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- df$date = as.Date(df$excel.date, format = "%d/%m/%Y")
- head(df$date)
- [1] NA NA NA "0006-01-05" NA NA
- 7/28/05
- 7/28/05
- 12/16/05
- 5/1/06
- 4/21/05
- head(df$excel.date)
- [1] 7/28/05 7/28/05 12/16/05 5/1/06 4/21/05 1/25/07
- 1079 Levels: 1/1/00 1/1/02 1/1/97 1/10/96 1/10/99 1/11/04 1/11/94 1/11/96 1/11/97 1/11/98 ... 9/9/99
- str(df)
- .
- .
- $ excel.date : Factor w/ 1079 levels "1/1/00","1/1/02",..: 869 869 288 618 561 48 710 1022 172 241 ...
- > as.Date("13/04/2014", format= "%d/%m/%Y")
- [1] "2014-04-13"
- > as.Date(factor("13/04/2014"), format= "%d/%m/%Y")
- [1] "2014-04-13"
- > as.Date(factor("13/04/14"), format= "%d/%m/%Y")
- [1] "14-04-13"
- > as.Date(factor("13/04/14"), format= "%d/%m/%y")
- [1] "2014-04-13"
- function (x, ...) as.Date(as.character(x), ...)
- x <- c("7/28/05", "7/28/05", "12/16/05", "5/1/06", "4/21/05", "1/25/07")
- repairExcelDates <- function(x, yearcol=3, fmt="%m/%d/%Y") {
- x <- do.call(rbind, lapply(strsplit(x, "/"), as.numeric))
- year <- x[,yearcol]
- if(any(year>99)) stop("dont'know what to do")
- x[,yearcol] <- ifelse(year <= as.numeric(format(Sys.Date(), "%Y")), year+2000, year + 1900)
- # if year <= current year then add 2000, otherwise add 1900
- x <- apply(x, 1, paste, collapse="/")
- as.Date(x, format=fmt)
- }
- repairExcelDates(x)
- # [1] "2005-07-28" "2005-07-28" "2005-12-16" "2006-05-01" "2005-04-21"
- # [6] "2007-01-25"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement