JC11
JC11

Reputation: 473

Formatting dates and times with as.POSIXct in R

I am attempting to format a column of dates and time (datetime) from my file "f845" using as.POSIXct in R. The file 845 has 21 lines to skip before formatting as seen in the first line of code below, it also has two columns and 100000 rows.

When I attempt to format the datetime column with the second line of code below I end up with the output as seen in the data below the 3rd line.

How come my code is not formatting the date and time properly?

> dat=read.table(f845,sep="\t",skip=21,col.names=c("datetime","light"))
> dat$datetime=as.POSIXct(strptime(dat$datetime,format="%d-%m-%Y %H:%M:%S", tz="UTC"))
> dat[1:10,]
   datetime  time
1      <NA> 4.542
2      <NA> 7.949
3      <NA> 5.678
4      <NA> 7.949
5      <NA> 7.949
6      <NA> 6.813
7      <NA> 2.271
8      <NA> 2.271
9      <NA> 2.271
10     <NA> 2.271

Here is what the data looks like before the formatting:

> dat=read.table(f845,sep="\t",skip=21,col.names=c("datetime","light"))
> dat[1:10,]
              datetime light
1  21/05/2013 22:56:07 4.542
2  21/05/2013 23:01:07 7.949
3  21/05/2013 23:06:07 5.678
4  21/05/2013 23:11:07 7.949
5  21/05/2013 23:16:07 7.949
6  21/05/2013 23:21:07 6.813
7  21/05/2013 23:26:07 2.271
8  21/05/2013 23:31:07 2.271
9  21/05/2013 23:36:07 2.271
10 21/05/2013 23:41:07 2.271

Edit: the output of dput(droplevels (dput(dat[1:10,])) is shown below, there is still a large number of dates so I only show the last few lines:

> dput(droplevels(dat[1:10,]))
structure(list(datetime = structure(1:10, .Label = c("21/05/2013 22:56:07", 
"21/05/2013 23:01:07", "21/05/2013 23:06:07", "21/05/2013 23:11:07", 
"21/05/2013 23:16:07", "21/05/2013 23:21:07", "21/05/2013 23:26:07", 
"21/05/2013 23:31:07", "21/05/2013 23:36:07", "21/05/2013 23:41:07"
), class = "factor"), light = c(4.542, 7.949, 5.678, 7.949, 7.949, 
6.813, 2.271, 2.271, 2.271, 2.271)), .Names = c("datetime", "light"
), row.names = c(NA, 10L), class = "data.frame")

Upvotes: 2

Views: 7003

Answers (1)

akrun
akrun

Reputation: 886948

The initial error might be due to using - instead of / in the format.

 dat <- read.table("D845.lux",sep="\t",skip=21,
        col.names=c("datetime","light"), stringsAsFactors=FALSE)

  dim(dat)
 #[1] 100247      2

  Datetime <- as.POSIXct(dat$datetime, format="%d/%m/%Y %H:%M:%S")
  head(Datetime)
  #[1] "2013-05-21 22:56:07 EDT" "2013-05-21 23:01:07 EDT"
  #[3] "2013-05-21 23:06:07 EDT" "2013-05-21 23:11:07 EDT"
  #[5] "2013-05-21 23:16:07 EDT" "2013-05-21 23:21:07 EDT"

  any(is.na(Datetime))
 #[1] FALSE

Upvotes: 1

Related Questions