I have data frame (test) that looks like this:
abx start stop abx2 start2 stop2 abx3 start3 stop3
cipro 07/10/12 07/10/12 flagyl 07/10/12 07/17/12 n/a n/a n/a
vanco 07/12/2012 07/15/2012 levo 07/20/2012 07/27/2012 n/a n/a n/a
This continues until start9/stop9. I'd like to convert all the dates to standard date format.
I wrote a function to convert the start dates depending on the number of characters in the date. Plan to write a similar function for the stops.
dateconv <- function(x) {
as.character(x)
z <- ifelse(nchar(x) == 8, "y","Y")
date <- as.Date(x, format =paste0("%m/%d/%", z))
rm(z)
}
test[,grep("^start", names(test))] <- dateconv(test[,grep("^start",
names(test))])
Any ideas what I'm doing wrong? Getting this error: Error in as.Date.default(x, format = paste0("%m/%d/%", z)) : do not know how to convert 'x' to class “Date”
Update (Feb 20, 2015): Thanks to Richard's comment I got this to work (really like the grep("st(art|op)" :
g <- grep("st(art|op)", names(test), value = TRUE)
test[g] <- lapply(test[g], function(x) {
x <- as.character(x)
as.Date(x, format = paste0("%m/%d/%", ifelse(nchar(x) == 8, "y", "Y")))})