0

我有一组以日期为列的数据。当我打开 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 TO HGR P 0010 A- NA 1 TOW OUT PUSH_TOW_OUT PUSH_TOW_OUT 22/02/2013 23:53 23/02/2013 00:21 星期六 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 拖出 EK0726--EK0650 NA E41---D03 4507 - TBL400 拖出 FM E41 TO 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 星期六 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 TO 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 TO HGR P-0030 A- NA 1 TOW OUT PUSH_TOW_OUT PUSH_TOW_OUT 23/02/2013 00:43 23/02/2013 01:32 星期六 23/02/2013 00:46 23/02/2013 01:03 17 完成 22/02/2013 22:47 23/02/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)
4

1 回答 1

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"
于 2013-04-17T09:42:44.263 回答