0
votes

I have a set of data with dates as columns. When I open csv file I see the format as below

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

But when I read this file and printout date format is as below:

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

I tried to convert this data into "ddmmyyyy" format using all the options listed I could find, listed below:

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

But nothing works. All are giving NA as result

Please advise, where I am going wrong.

My data is below: 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 Saturday 23/02/2013 00:16 23/02/2013 00:21 5 Finished 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 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 Saturday 23/02/2013 00:08 23/02/2013 00:25 17 Finished 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 Saturday 23/02/2013 01:13 23/02/2013 01:26 13 Finished 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 Saturday 23/02/2013 00:46 23/02/2013 01:03 17 Finished 22/02/2013 22:47 23/02/2013 11:20 23/02/2013 11:43 22/02/2013 23:10

My code is below:

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
What do you use to open the file?James
You have'nt told that what output you want to achieve? Explain your question will help more to answer. What I get it instead of trying 'date format' just use '%s' might work.KumailR
I have other data elements along with date. There are total of 22 columns in my data. I am using read.csv(file.choose()) to open the file. I want my output to show 22/02/2013 23:10 as "22022013 "Chandra

1 Answers

1
votes

Try this:

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"