【发布时间】:2019-05-15 21:05:02
【问题描述】:
我在这里尝试了多种可能的解决方案,但仍然无法根据时间过滤一列。我使用 lubridate 将列变成 hms。请指教。 R 新手,如果这看起来多余,请道歉。
我尝试了过滤器命令,但似乎不起作用,因为数据类型错误。字符与双重。我认为由于 lubridate 代码而出现了双倍,但我在这里可能是错的。下面是相关部分的代码示例。
这是数据集的顶部供参考。
> head(iislog1,n=10)
iisdate iistime csUriStem timeTaken
1 2019-05-10 4H 35M 10S /claraportal 7375
2 2019-05-10 4H 35M 11S /claraportal 484
3 2019-05-10 4H 35M 11S /claraportal 468
4 2019-05-10 4H 35M 13S /claraportal 1024
5 2019-05-10 4H 35M 54S /claraportal 5765
6 2019-05-10 4H 35M 57S /claraportal/content/bundle 2019
7 2019-05-10 4H 35M 57S /claraportal/dashboard.fwk.style/bundle 2019
8 2019-05-10 4H 35M 57S /claraportal/bundle/css/modules/2019v1_v1 2238
9 2019-05-10 4H 35M 57S /claraportal/scripts/thirdparty 2457
10 2019-05-10 4H 35M 58S /claraportal/content/bundle 921
#change data type for date and time columns
iislog$iisdate <- ymd(iislog$iisdate)
iislog$iistime <- hms(iislog$iistime)
#create subset of the original data
iislog1 <- iislog %>% select(iisdate,iistime,csUriStem,timeTaken)
#ensure the csUriStem column is in all lowercase. This is because the URLs
#seem to have mixed case and therefore can show up moe than once.
iislog1$csUriStem <- tolower(iislog1$csUriStem)
#filter the rows to find times between
iislog1 <- filter(iislog1$iistime > '04:40:59' & iislog1$iistime < '21:38:37')
> iislog1 <- filter(iislog1$iistime > '04:40:59' & iislog1$iistime < '21:38:37')
Error in UseMethod("filter_") :
no applicable method for 'filter_' applied to an object of class "logical"
【问题讨论】:
-
当前您正在将日期类与字符串进行比较,请将您的日期/时间字符串转换为日期类。
-
用另一种方式说明 zx8754 的观点:
iislog1$iistime可能是类datetime,但'04:40:59'只是一个character -
其实
iislog$iistime转换后iislog$iistime <- hms(iislog$iistime)就是类Period(见??hms)。因此,为了比较,请将您的时间字符串转换为hms():iislog1.filtered <- filter(iislog1, iistime > hms('04:35:10') & iistime < hms('21:38:37')) -
我会试试的。所以,基本上,你的意思是甚至将它转换为 hms 都没有意义?如果我只是保留最初的数据,这可能是字符,我会没事的吗?
标签: r datetime dplyr lubridate