用于格式化没有做任何事情的日期

时间:2015-06-01 09:33:19

标签: r

我编写了一个应该采用数据集并将所选列格式化为日期格式的函数:

auto_date<-function(data,date){
  try(if (is.Date(data$date)==TRUE )stop("date col already recognised as a date") )


  ifelse( is.na (as.Date (paste (data$date),format="%d/%m/%Y")),data$date, data$date<-as.Date(paste (data$date),format="%d/%m/%Y") ) 
  ifelse( is.na( as.Date (paste (data$date),format="%d-%m-%Y")),data$date, data$date<-as.Date(paste (data$date),format="%d-%m-%Y") )
  ifelse( is.na(as.Date (paste (data$date),format="%Y-%m-%d")),data$date, data$date<-as.Date(paste (data$date),format="%Y-%m-%d") ) 


  return(data)
  }

因此该函数检查第一个as.Date中指定的格式是否返回NA,并相应地起作用。

如果我按这样调用函数:

test<-auto_date(data,"date") 

它运行时不会返回任何错误,但它不会将所选日期列更改为Date类。

我是否错误地使用了ifelse?

一些数据的输入:

structure(list(x= c(5.52414725524019, 5.51568697826017, 
5.50717840676967, 5.49854187230171, 5.48968334957573, 5.48051172956661, 
5.47095653655004, 5.4609862713426, 5.45062752168778, 5.43998490278634, 
5.42926177479571, 5.41858152806773, 5.40800902940464, 5.39757198255256, 
5.3872820731988, 5.37715584306555, 5.36723526972862, 5.35760801907769, 
5.34842728646297, 5.33993105119651, 5.33226043801745, 5.32547670956252, 
5.31957660759245, 5.3145059204488, 5.31017128325798, 5.30645031930548, 
5.30320030931028, 5.30026563335276, 5.29748427089467, 5.29469367013105, 
5.29173631071517, 5.28846528496001, 5.28475021255715, 5.28048378262823, 
5.27558918386977, 5.27002863741307, 5.26381318602165, 5.25701381518421, 
5.24977388401744, 5.24232272400762, 5.23499011887304, 5.22802120688394, 
5.22159114607283, 5.21581905205846, 5.2107808581674, 5.20652085962214, 
5.20306178992882, 5.20041334087433, 5.1985790806657, 5.19756175082822, 
5.19736693472586, 5.19800509222092, 5.19949194930623, 5.20184722179022, 
5.20509164158265, 5.20924224614975, 5.21430588967455, 5.22027094184779, 
5.22709716058088, 5.23490376190152, 5.24395576751624, 5.25464839160778, 
5.26748893682384, 5.28287540902893, 5.30107083621387, 5.32217249555146, 
5.34607552254204, 5.37243070769488, 5.40099669047415, 5.43158724892862, 
5.46401416772613, 5.49802605703665, 5.53344349869472, 5.57009102242077, 
5.60772627093789, 5.64596670492428, 5.68461433251628, 5.7235792284404, 
5.76280223875809, 5.80217705107394, 5.84147174851502, 5.88065006010624, 
5.9197927713755, 5.95901936773047, 5.99840974903606, 6.03812577642173, 
6.07833249151887, 6.11911868440602, 6.1604164801787, 6.20231976656359, 
6.24500159832704, 6.28863039070223, 6.33328455572572, 6.37886524465265, 
6.4254070396267, 6.47298679230227, 6.52163054025447, 6.57121834747384, 
6.62178701687038, 6.67343091465917, 6.72620083302154, 6.78000070230948, 
6.83488205120897, 6.89093640700136, 6.94818553286558, 7.00646931916657, 
7.06533128520351, 7.1245020116373, 7.18378141655838, 7.24292041403394, 
7.30150235529807, 7.35922475075675, 7.41578210610541, 7.47075047753792, 
7.52347435703599, 7.57355673562621, 7.62075365562113, 7.66487404586203, 
7.70568532787896, 7.74282517511024, 7.77611989033843, 7.80551012599586, 
7.83098429473421, 7.85251978895315, 7.87023203246288, 7.88433140949571, 
7.89508784021901, 7.90280258198116, 7.90778671645011, 7.91034572012985, 
7.91076949609146, 7.90932725596577, 7.9062666723914, 7.90181676349508, 
7.89619401405871, 7.88961127533721, 7.88228901055741, 7.87446846039543, 
7.8664262875063, 7.85849021762423, 7.85105512390845, 7.84459889908293, 
7.8394973256238, 7.83603698820906, 7.83442547686543, 7.83479830764323, 
7.83722214563906, 7.84169405933421, 7.8481366729187, 7.8563892224639, 
7.86619467069532, 7.87718320189357, 7.88885261099784, 7.90054632654356, 
7.91143007184415, 7.92086847755994, 7.92840331420758, 7.9337346153486, 
7.93670560791611, 7.93729204961254, 7.93559628692445, 7.93184608223923, 
7.92639800537038, 7.91954493396692, 7.91152694966716, 7.90254504424145, 
7.8927771555608, 7.88239613140775, 7.87158926525371, 7.86057905830728, 
7.84964483324656, 7.83914475463303, 7.82933769736167, 7.82040424681821, 
7.81246631299364, 7.80560499738534, 7.79987646886467, 7.79532568630825, 
7.79199785565496, 7.78994753147105, 7.78924527261148, 7.78998174286825, 
7.79226911548816, 7.79623960023226, 7.80204086863062, 7.80962811301286, 
7.81875244397133, 7.82894571587235, 7.83950227819432, 7.84985858898386, 
7.85957210173123, 7.8683005527487, 7.8757825341451, 7.88182003692375, 
7.88626348701966, 7.88899967029404, 7.88994284563403, 7.88902927258619, 
7.88621532500581, 7.88147931820303, 7.87482713683796, 7.86630170693429, 
7.85599630024206, 7.84407158509743, 7.83077623723606, 7.81647078922346, 
7.80165422115063, 7.78679257153014, 7.77234857043227, 7.75881136278422, 
7.74672539637235, 7.73651749625744, 7.72852103670752, 7.72299635699811, 
7.72014675433134, 7.72012953319531, 7.72306170456948, 7.72902002036692, 
7.73803510909154, 7.75007955920654, 7.76504988895692, 7.78274245746929, 
7.8028235240923, 7.82479386325971, 7.84834860235705, 7.87333328104337, 
7.89969674177296, 7.92744116043806, 7.95656931944734, 7.9870291188886, 
8.01865532021076, 8.05110862662494, 8.08381243070185, 8.11628790775895, 
8.14808860825228, 8.1787355068224, 8.20765340089335, 8.2345096168755, 
8.25915617437749, 8.28157607377416, 8.30183400309162, 8.32003149820843, 
8.33626642699697, 8.35059659433318, 8.36300727223904, 8.37338253954783, 
8.38168046054839, 8.38791233445042, 8.39212609954255, 8.39439386944449, 
8.39480350557689, 8.39345408162148, 8.39045506424885, 8.38592901196637, 
8.38001757315287, 8.37289053816328, 8.36475766220622, 8.35588291941396, 
8.34660076859438, 8.33713390271652, 8.32761181321224), y= 1:261, 
    date = c("2007-01-01", "2007-01-08", "2007-01-15", "2007-01-22", 
    "2007-01-29", "2007-02-05", "2007-02-12", "2007-02-19", "2007-02-26", 
    "2007-03-05", "2007-03-12", "2007-03-19", "2007-03-26", "2007-04-02", 
    "2007-04-09", "2007-04-16", "2007-04-23", "2007-04-30", "2007-05-07", 
    "2007-05-14", "2007-05-21", "2007-05-28", "2007-06-04", "2007-06-11", 
    "2007-06-18", "2007-06-25", "2007-07-02", "2007-07-09", "2007-07-16", 
    "2007-07-23", "2007-07-30", "2007-08-06", "2007-08-13", "2007-08-20", 
    "2007-08-27", "2007-09-03", "2007-09-10", "2007-09-17", "2007-09-24", 
    "2007-10-01", "2007-10-08", "2007-10-15", "2007-10-22", "2007-10-29", 
    "2007-11-05", "2007-11-12", "2007-11-19", "2007-11-26", "2007-12-03", 
    "2007-12-10", "2007-12-17", "2007-12-24", "2007-12-31", "2008-01-07", 
    "2008-01-14", "2008-01-21", "2008-01-28", "2008-02-04", "2008-02-11", 
    "2008-02-18", "2008-02-25", "2008-03-03", "2008-03-10", "2008-03-17", 
    "2008-03-24", "2008-03-31", "2008-04-07", "2008-04-14", "2008-04-21", 
    "2008-04-28", "2008-05-05", "2008-05-12", "2008-05-19", "2008-05-26", 
    "2008-06-02", "2008-06-09", "2008-06-16", "2008-06-23", "2008-06-30", 
    "2008-07-07", "2008-07-14", "2008-07-21", "2008-07-28", "2008-08-04", 
    "2008-08-11", "2008-08-18", "2008-08-25", "2008-09-01", "2008-09-08", 
    "2008-09-15", "2008-09-22", "2008-09-29", "2008-10-06", "2008-10-13", 
    "2008-10-20", "2008-10-27", "2008-11-03", "2008-11-10", "2008-11-17", 
    "2008-11-24", "2008-12-01", "2008-12-08", "2008-12-15", "2008-12-22", 
    "2008-12-29", "2009-01-05", "2009-01-12", "2009-01-19", "2009-01-26", 
    "2009-02-02", "2009-02-09", "2009-02-16", "2009-02-23", "2009-03-02", 
    "2009-03-09", "2009-03-16", "2009-03-23", "2009-03-30", "2009-04-06", 
    "2009-04-13", "2009-04-20", "2009-04-27", "2009-05-04", "2009-05-11", 
    "2009-05-18", "2009-05-25", "2009-06-01", "2009-06-08", "2009-06-15", 
    "2009-06-22", "2009-06-29", "2009-07-06", "2009-07-13", "2009-07-20", 
    "2009-07-27", "2009-08-03", "2009-08-10", "2009-08-17", "2009-08-24", 
    "2009-08-31", "2009-09-07", "2009-09-14", "2009-09-21", "2009-09-28", 
    "2009-10-05", "2009-10-12", "2009-10-19", "2009-10-26", "2009-11-02", 
    "2009-11-09", "2009-11-16", "2009-11-23", "2009-11-30", "2009-12-07", 
    "2009-12-14", "2009-12-21", "2009-12-28", "2010-01-04", "2010-01-11", 
    "2010-01-18", "2010-01-25", "2010-02-01", "2010-02-08", "2010-02-15", 
    "2010-02-22", "2010-03-01", "2010-03-08", "2010-03-15", "2010-03-22", 
    "2010-03-29", "2010-04-05", "2010-04-12", "2010-04-19", "2010-04-26", 
    "2010-05-03", "2010-05-10", "2010-05-17", "2010-05-24", "2010-05-31", 
    "2010-06-07", "2010-06-14", "2010-06-21", "2010-06-28", "2010-07-05", 
    "2010-07-12", "2010-07-19", "2010-07-26", "2010-08-02", "2010-08-09", 
    "2010-08-16", "2010-08-23", "2010-08-30", "2010-09-06", "2010-09-13", 
    "2010-09-20", "2010-09-27", "2010-10-04", "2010-10-11", "2010-10-18", 
    "2010-10-25", "2010-11-01", "2010-11-08", "2010-11-15", "2010-11-22", 
    "2010-11-29", "2010-12-06", "2010-12-13", "2010-12-20", "2010-12-27", 
    "2011-01-03", "2011-01-10", "2011-01-17", "2011-01-24", "2011-01-31", 
    "2011-02-07", "2011-02-14", "2011-02-21", "2011-02-28", "2011-03-07", 
    "2011-03-14", "2011-03-21", "2011-03-28", "2011-04-04", "2011-04-11", 
    "2011-04-18", "2011-04-25", "2011-05-02", "2011-05-09", "2011-05-16", 
    "2011-05-23", "2011-05-30", "2011-06-06", "2011-06-13", "2011-06-20", 
    "2011-06-27", "2011-07-04", "2011-07-11", "2011-07-18", "2011-07-25", 
    "2011-08-01", "2011-08-08", "2011-08-15", "2011-08-22", "2011-08-29", 
    "2011-09-05", "2011-09-12", "2011-09-19", "2011-09-26", "2011-10-03", 
    "2011-10-10", "2011-10-17", "2011-10-24", "2011-10-31", "2011-11-07", 
    "2011-11-14", "2011-11-21", "2011-11-28", "2011-12-05", "2011-12-12", 
    "2011-12-19", "2011-12-26")), .Names = c("x", 
"y", "date"), row.names = c(NA, -261L), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

我不相信您可以使用date在数据框中引用列data$date,其中date是字符串变量。相反,您必须使用data[[date]]data[, date]。请尝试使用此代码:

auto_date <- function(data,date) {
    if (is.Date(data[[date]])) {
        print("date col already recognised as a date")
        return data[[date]]
    }

    if (!is.na (as.Date (paste (data[[date]]),format="%d/%m/%Y"))) {
        return as.Date(paste (data[[date]]),format="%d/%m/%Y") )
    }
    if (!is.na (as.Date (paste (data[[date]]),format="%d-%m-%Y"))) {
        return as.Date(paste (data[[date]]),format="%d-%m%-Y") )
    }
    if (!is.na (as.Date (paste (data[[date]]),format="%Y-%m-%d"))) {
        return as.Date(paste (data[[date]]),format="%Y-%m-%d") )
    }

    return data[[date]]
}

<强>用法:

data$date <- auto_date(data,"date")