I would like to use POSIXlt for date/time column in my data.frame and the filter() function to filter it. My code is below.
I believe that I have properly converted the date data to numerical but still I get an error.
How can I sort it out without changing POSIXlt to POSIXct?
require(dplyr)
x <- "2017-03-30"
y <- "2017-04-04"
name <- c("a","b","c","d")
weight <- c(2,3,1,5)
t <- c("2017-03-27 08:13:17", "2017-03-30 01:05:01", "2017-04-03 02:43:35", "2017-05-27 23:13:03")
z <- data.frame(name, weight, t)
z$t <- as.POSIXlt(z$t, tz="GMT", "%Y-%m-%d %H:%M:%S")
str(z)
z$t
x <- unclass(as.Date(x)) # integer
str(x)
y <- unclass(as.Date(y)) # integer
str(y)
d <- filter(z, between(unclass(as.Date(t)), x, y))
The error this throws:
Error: Column t
is a date/time and must be stored as POSIXct, not POSIXlt.
The dplyr
classes does not handle POSIXlt
columns (which are list
objects internally), while they can deal with POSIXct
objects, which are just numbers.
If you intend to stick with dplyr
you have to convert the t
column:
z$t<-as.POSIXct(z$t)
#now this works
d <- filter(z, between(as.Date(t), x, y))
If you don't want to coerce the column, you can subset with standard base operators:
z[as.Date(z$t)>=x & as.Date(z$t)<=y,]