gpt4 book ai didi

r - 如何在 R 中正确过滤日期

转载 作者:行者123 更新时间:2023-12-02 00:02:45 25 4
gpt4 key购买 nike

您好,我正在使用图书馆(carData)-MplsStops,我想过滤 2017 年 7 月在 Cedar Riverside、St. Anthony East、Downtown West 附近发生的所有事件,然后按纬度和经度排列它们。

日期格式如下:2017-01-01 00:00:42

我正在使用 dplyr

现在我正在尝试让这段代码工作:

MplsStops %>% 
filter(neighborhood=="Cedar Riverside" | neighborhood =="St. Anthony East" | neighborhood =="Downtown West") %>%
filter(date==2017-07) %>%
arrange(lat,long)

我认为date==有一些问题,任何人都可以给我任何如何让它工作的提示

最佳答案

一种选择是将 == 更改为 %in%格式化“日期”以执行 ==

library(dplyr)
library(carData)
MplsStops %>%
filter(neighborhood %in% c("Cedar Riverside", "St. Anthony East",
"Downtown West")) %>%
filter(format(as.Date(date), "%Y-%m") == "2017-07") %>%
arrange(lat, long)
# idNum date problem MDC citationIssued personSearch vehicleSearch preRace race gender
#1 17-264432 2017-07-14 22:07:37 traffic MDC NO NO NO Unknown Black Male
#2 17-274061 2017-07-21 13:17:05 suspicious MDC YES NO NO Native American Native American Female
#3 17-252658 2017-07-06 23:29:22 traffic MDC NO NO NO Unknown East African Male
#4 17-250572 2017-07-05 18:31:16 suspicious MDC <NA> NO NO Black Black Male
#5 17-269530 2017-07-18 17:03:38 traffic other <NA> <NA> <NA> <NA> <NA> <NA>
#6 17-277463 2017-07-23 22:05:45 traffic MDC NO NO
NO Black Black Male
#...
# lat long policePrecinct neighborhood
#1 44.96437 -93.24308 1 Cedar Riverside
#2 44.96440 -93.23357 1 Cedar Riverside
#3 44.96466 -93.23616 1 Cedar Riverside
#4 44.96497 -93.23492 1 Cedar Riverside
#5 44.96497 -93.23492 1 Cedar Riverside
#6 44.96497 -93.23492 1 Cedar Riverside
#...

关于r - 如何在 R 中正确过滤日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59555263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com