将日期格式转换为ddmmyyyy

时间:2013-04-17 09:34:31

标签: r date format


我有一组数据,日期为列。当我打开csv文件时,我看到如下格式

22/02/2013 23:10    
22/02/2013 23:20   
23/02/2013 00:20   
22/02/2013 23:10   
22/02/2013 23:59

但是当我读到这个文件时,打印输出日期格式如下:

> head(tow$X22)
[1] "23:10 / 22-Feb" "23:20 / 22-Feb" "00:20 / 23-Feb" "23:10 / 22-Feb"
[5] "23:59 / 22-Feb" "23:15 / 22-Feb"

我尝试使用我能找到的所有选项将此数据转换为“ddmmyyyy”格式,如下所示:

  tow$X22 <- as.Date(as.character(tow$X22),format="%d-%m-%y")      
  tow$X22 <- strptime(tow$X22,"%d%m%Y")        
  tow$X22 <- strftime(tow$X22,"%d%m%Y")    
  tow$X22 <- strftime(as.character(tow$X22),"%d%m%Y")     
  tow$X22 <- as.Date(tow$X22,"%d%m%Y")      

但没有任何作用。所有人都给出NA作为结果

请告知我哪里出错了。

我的数据如下: X01 X02 X03 X04 X05 X06 X07 X08 X09 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 X21 X22 TOW OUT EK0134 - EK0705 NA C40 --- D02 4318 - TBL600 TOW OUT FM C40至HGR P 0010 A-NA 1 TOW OUT PUSH_TOW_OUT PUSH_TOW_OUT 22/02/2013 23:53 23/02/2013 00:21 Saturday 23/02/2013 00:16 23/02/2013 00:21 5完成22/02/2013 22:55 24/02 / 2013 02:30 24/02/2013 02:44 22/02/2013 23:10 TOW OUT EK0726 - EK0650 NA E41 --- D03 4507 - TBL400 TOW OUT FM E41至HGR P 0001 A-0008/0025 NA 1 TOW OUT PUSH_TOW_OUT PUSH_TOW_OUT 22/02/2013 23:55 23/02/2013 00:13 Saturday 23/02/2013 00:08 23/02/2013 00:25 17完成22/02/2013 23:19 24/02 / 2013 02:45 24/02/2013 02:59 22/02/2013 23:20 TOW IN EK0176 - EK0658 NA D02 --- B24 4508 - TBL400 TOW IN D02至B24 P-0045 A-NA 1 TOW IN PUSH_TOW_DEP PUSH_TOW_DEP 23/02/2013 00:36 23/02/2013 01:18星期六23/02/2013 01:13 23/02/2013 01:26 13完成22/02/2013 23:48 23/02/2013 03:15 23/02/2013 03:25 23/02/2013 00:20 TOW OUT EK0383 - EK0308 NA E26 --- A03 4507 - TBL400 TOW OUT FM E26至HGR P-00 30 A- NA 1 TOW OUT PUSH_TOW_OUT PUSH_TOW_OUT 23/02/2013 00:43 23/02/2013 01:32 Saturday 23/02/2013 00:46 23/02/2013 01:03 17完成22/02/2013 22 :47 23 23/2013 11:20 23/02/2013 11:43 22/02/2013 23:10

我的代码如下:

tow <-read.csv(file.choose(),skip=3)   
tow<-subset(tow,tow[1]!="Special Task")   
#SPLITTING FLIGHT NUMBERS AND ADDING TO DATA FRAME
a<-data.frame(do.call(rbind,strsplit(as.character(tow$X02),"--")))
if(nrow(tow)!=nrow(a)) cat("ERROR IN FLIGHT NUMBERS *** PLEASE CHECK","\n")
names(a)<-c("Arr","Dep")
tow<-cbind(tow,a)


#IDENTIFYING & REMOVING COLUMNS WHICH ARE NOT HAVING ARRIVAL & DEPARTURE TIMES
 tow<-subset(tow,tow[20]!="")
 a<-data.frame(do.call(rbind,strsplit(as.character(tow$X20)," ")))
 if(nrow(tow)!=nrow(a)) cat("ERROR IN DEPARTURE DATE & TIME *** PLEASE CHECK","\n")
 names(a)<-c("DD","DT")
 tow<-cbind(tow,a)

tow<-subset(tow,tow[22]!="")
a<-data.frame(do.call(rbind,strsplit(as.character(tow$X22)," ")))
if(nrow(tow)!=nrow(a)) cat("ERROR IN ARRIVAL DATE & TIME *** PLEASE CHECK","\n")
names(a)<-c("DD","DT")
tow<-cbind(tow,a)

#IDENTIFYING & REMOVING COLUMNS WHICH ARE NOT HAVING ARRIVAL & DEPARTURE TIMES
tow<-subset(tow,tow[20]!="")
a<-data.frame(do.call(rbind,strsplit(as.character(tow$X20)," ")))
if(nrow(tow)!=nrow(a)) cat("ERROR IN DEPARTURE DATE & TIME *** PLEASE CHECK","\n")
names(a)<-c("DD","DT")
tow<-cbind(tow,a)

tow<-subset(tow,tow[22]!="")
a<-data.frame(do.call(rbind,strsplit(as.character(tow$X22)," ")))
if(nrow(tow)!=nrow(a)) cat("ERROR IN ARRIVAL DATE & TIME *** PLEASE CHECK","\n")
names(a)<-c("DD","DT")
tow<-cbind(tow,a)

1 个答案:

答案 0 :(得分:1)

试试这个:

df <- read.table(text=
"22/02/2013 23:10
22/02/2013 23:20
23/02/2013 00:20
22/02/2013 23:10
22/02/2013 23:59", stringsAsFactors=F)

strptime(df$V1, "%d/%m/%Y")
[1] "2013-02-22" "2013-02-22" "2013-02-23" "2013-02-22" "2013-02-22"