Convert dates with different formats in data frame

2019-09-11 02:44发布

问题:

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")))})

回答1:

Here's one way to change the test data frame.

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")))
}))

which gives

#     abx      start       stop   abx2     start2      stop2 abx3 start3 stop3
# 1 cipro 2012-07-10 2012-07-10 flagyl 2012-07-10 2012-07-17  n/a   <NA>  <NA>
# 2 vanco 2012-07-12 2012-07-15   levo 2012-07-20 2012-07-27  n/a   <NA>  <NA>

Where test is

test <- read.table(text = "abx   start      stop       abx2    start2     stop2     abx3 start3 stop3\n    cipro 07/10/12   07/10/12   flagyl  07/10/12   07/17/12   n/a   n/a   n/a\n    vanco 07/12/2012 07/15/2012 levo    07/20/2012 07/27/2012 n/a  n/a    n/a", header = TRUE, stringsAsFactors=FALSE)


回答2:

Your easiest bet is to use mdy() from the lubridate package:

library(lubridate)
dateFields <- grep('^(start|stop)',names(test),value=T)
test[,dateFields] <- lapply(test[,dateFields],mdy)


标签: r as.date