简体   繁体   中英

Filter rows according to date variable with dplyr

I would like to filter a data.frame either by the column "Date" or "ID".

date <- seq(as.POSIXct(strptime("2016-02-01 23:59:59", format = "%Y-%m-%d %H:%M:%S")),by="month",length.out=3)-86400
df <- data.frame(Date = date, ID = c(1, 2, 3))
select <- "ID"

df %>% filter_(paste(select, "==", 1))

This works fine, however when I try to filter for the POSIXct-Date:

select <- "Date"
df %>% filter_(paste(select, "==", date[1]))

I get an error message. Any idea how I can get it to work?

Since there are spaces in your date object, it must be quoted. One quick workaround would be this:

df %>% filter_(paste(select, "== \'", date[1],"\'"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM