R-如何从当前数据中检索先前的一天数据

时间:2017-10-25 05:00:24

标签: r mongolite

我正在使用mongolite包来连接和检索MongoDB中的数据,请帮我从当前日期和时间中检索最后一天的数据。

##connecting mongodb

library(mongolite)

mongo<-mongolite::mongo(collection = "Sample", db = "Test", url = 
                          "mongodb://User:123@Wyyuyu:13333/ty2_U",verbose = TRUE)

## getting  data from collection

values <- mongo$find()

以上代码步骤可以正常运行以获取样本集合中的所有数据。但我的需求是只从当前的数据和时间中获取前一天的数据。

示例数据框。

Get <- data.frame(id = c(1,2,3,4,5,6),
                 firstName=c("kannan","anderson","jimmy","aray","stepen","james"),
                 dates =as.POSIXct(c("2017-10-25 15:10:59","2017-10-25 11:30:59 ","2017-10-24 15:30:59 ","2017-10-23 15:32:33","2017-10-24 11:22:34","2017-10-23 14:25:17")))

输出数据框

id   name      dte 
3   jimmy     2017-10-24 15:30:59

5   stepen     2017-10-24 11:22:34

1 个答案:

答案 0 :(得分:1)

您可以像这样过滤数据

Get <- data.frame(id = c(1,2,3,4,5,6),
                  firstName=c("kannan","anderson","jimmy","aray","stepen","james"),
                  dates =as.POSIXct(c("2017-10-25 15:10:59","2017-10-25 11:30:59 ","2017-10-24 15:30:59 ","2017-10-23 15:32:33","2017-10-24 11:22:34","2017-10-23 14:25:17")))


Get[Get$dates > (as.Date(Sys.time()) - 1) & Get$dates < as.Date(Sys.time()),]

  id firstName               dates
3  3     jimmy 2017-10-24 15:30:59
5  5    stepen 2017-10-24 11:22:34