【问题标题】:R-How to retrieve previous one day data from present day dataR-如何从今天的数据中检索前一天的数据
【发布时间】:2018-04-06 01:32:54
【问题描述】:

我正在使用 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

【问题讨论】:

  • @Hardikgupta,感谢您的回复。我在下面的帖子中尝试过,但我得到了以下结果。 [1] id firstName dates (或 0-length row.names) 警告信息:1: In [.data.frame(Get, Get$dates > (as.Date(Sys.time()) - 1) & Get$dates " 2: In [.data.frame(Get, Get$dates > (as.Date(Sys.time()) - 1 ) & Get$dates 的不兼容方法 ("Ops.POSIXt", "Ops.Date")
  • 只返回空数据框。 [1] id firstName dates (或 0-length row.names) 。我需要转换任何时区吗?
  • 你能检查你的 str(Get) 并发布结果吗,因为当我检查结果是正确的时候
  • 'data.frame':6 obs。 3 个变量:$ id:num 1 2 3 4 5 6 $ firstName:因子 w/ 6 个级别“anderson”,“aray”,..:5 1 4 2 6 3 $ 日期:POSIXct,格式:“2017-10 -25 15:10:59" "2017-10-25 11:30:59" "2017-10-24 15:30:59" "2017-10-23 15:32:33" ...
  • 如果我像 print(Get$dates) 这样打印结果,我得到 [1] "2017-10-25 15:10:59 IST" "2017-10-25 11: 30:59 IST" "2017-10-24 15:30:59 IST" "2017-10-23 15:32:33 IST" "2017-10-24 11:22:34 IST" [6] "2017- 10-23 14:25:17 IST"

标签: r mongolite


【解决方案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

【讨论】:

  • 这仍然给您带来问题吗?在这里你的例子工作得很好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-18
  • 1970-01-01
  • 2021-10-13
相关资源
最近更新 更多