Question

I have a large dataset that in the first two columns contain the Date/Time information in character type.

        Date     Time                    Current
1 28.02.2013 23:58:50                       NA
2 28.02.2013 23:58:50                      0.3
3 28.02.2013 23:58:51                      0.3
4 28.02.2013 23:58:51                      0.3
5 28.02.2013 23:58:57                      0.3
6 28.02.2013 23:58:58                      0.3

Since I want to convert it to an xts object later on, I transform these two columns into one of the type POSIXct containing the Date and Time with the following code:

bigdata <- ldply (files, read.table,header=TRUE, sep = "\t", dec=".", as.is= 1:2 ) #read the tables keeping the charater format of the first two columns 
library("chron")
Time<-chron(bigdata$Date, bigdata$Time, format= c(dates= "d.m.y", times="h:m:s")) 
Time2<- as.POSIXct(paste(as.Date(dates(Time)),times(Time)%%1)) #to POSIXct

The result in Time2 is a POSIXct object with the date and the time in a single column, and giving the desired result:

"2013-02-01 00:02:09 CET" "2013-02-01 00:02:12 CET" "2013-02-01 00:02:13 CET" "2013-02-01 00:02:13 CET" "2013-02-01 00:02:13 CET".....

(I know the times are different since I used a previous result, but the idea is the same)

However for some strange reason, with some datasets of the same characteristic as the one I showed before, the result I am getting for Time2 turns out to be:

"2013-02-28 CET" "2013-02-28 CET" "2013-02-28 CET" "2013-02-28 CET" "2013-02-28 CET" "2013-02-28 CET"

(this time the times are the same as the extract from the dataset showed above)

The "Time" part is completely erased leaving just the dates. I have applied the same code to various of the datasets and for the majority it works just fine, but then one of them present this problem.

Any idea of why I am obtained the different result?.

---------------------------EDIT---------------------------------------

I have run the command dput on both objects Time and Time 2. Hera are the results:

> dput( head( Time2))
structure(c(1362006000, 1362006000, 1362006000, 1362006000, 1362006000, 
1362006000), class = c("POSIXct", "POSIXt"), tzone = "")

> dput( head( Time))
structure(c(15764.9991898148, 15764.9991898148, 15764.9992013889, 
15764.9992013889, 15764.9992708333, 15764.9992824074), format = structure(c("d.m.y", 
"h:m:s"), .Names = c("dates", "times")), origin = structure(c(1, 
1, 1970), .Names = c("month", "day", "year")), class = c("chron", 
"dates", "times"))
Was it helpful?

Solution

Why not instead just do it in one operation and not go through a chron-class object?

Time2<-as.POSIXct(paste(bigdata$Date, bigdata$Time), 
                  format= "%d.%m.%Y %H:%M:%S")

##########
bigdata <- read.table(text="    Date     Time                    Current
1 28.02.2013 23:58:50                       NA
2 28.02.2013 23:58:50                      0.3
3 28.02.2013 23:58:51                      0.3
4 28.02.2013 23:58:51                      0.3
5 28.02.2013 23:58:57                      0.3
6 28.02.2013 23:58:58                      0.3", header=TRUE, 
      stringsAsFactors=FALSE)

library("chron")
Time<-chron(bigdata$Date, bigdata$Time, format= c(dates= "d.m.y", times="h:m:s")) 
Time2<- as.POSIXct(paste(as.Date(dates(Time)),times(Time)%%1))
Time2
[1] "2013-02-28 23:58:50 PST" "2013-02-28 23:58:50 PST" "2013-02-28 23:58:51 PST"
[4] "2013-02-28 23:58:51 PST" "2013-02-28 23:58:57 PST" "2013-02-28 23:58:58 PST"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top