列中的过滤时间
Filter Time in Column
我在这里尝试了多种可能的解决方案,但仍然无法根据时间过滤一列。我使用 lubridate 将列转换为 hms。请指教。 R 的新手,如果这看起来多余,请见谅。
我试过过滤命令,但似乎不起作用,因为数据类型不对。性格对双。我认为 double 出现是因为 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"
以下是获得正确格式所需的内容:
iislog1$dtime <- with( iislog1, strptime(paste( iisdate,iistime),
format="%Y-%m-%d %HH %MM %SS"))
我认为您的输入格式与 hms
函数所接受的任何典型协议都不够接近。 Base R 更 "complete".
然后使用适当的"datetime"值进行比较。或者,如果您想要一个时间范围而不考虑日期,请只使用 format
到 return 时间并进行 alpha 比较。在您的实例中,'21:38:37' 与 '4:40:59' 的 alpha 比较将显示前者是 "less than" 后者,因为前导字母是“4”和“2”。正确构造的 R 日期时间中“4:40:59”的格式化版本为“04:40:59”。
你只是没有传递要过滤的数据框,你传递了一个向量。注意区别
# failes
iislog1 <- filter(iislog1$iistime > '04:40:59' & iislog1$iistime < '21:38:37')
# works |--missing--|
iislog1 <- iislog1 %>% filter(iislog1$iistime > '04:40:59' & iislog1$iistime < '21:38:37')
tidyverse
比基础 R 更简洁。最大的节省在于管道,而不必每次都命名数据框(即 df$col
)。你 可以 但你需要先传递一些东西,例如你不能做
df %>% filter(df$col < 2)
但这本身
filter(df$col < 2)
这是因为所有 dplyr
动词都希望第一个参数是要传递的内容,然后是 return 数据框。这三件事是一样的
filter(df, col < 2)
df %>% filter(., col < 2)
df %>% filter(col < 2)
所以 filter()
需要一个数据帧,而您向它传递了一个向量 df$col
,它不知道该怎么做。我希望这个解释是有道理的。 R for Data Science 这本书是一个很好的资源,而且是免费的。
总而言之,完成所有步骤的最佳方式如下
library(tidyverse)
library(lubridate)
raw_data <-
tibble(
iisdate = "2019-05-10",
iistime = paste0(1:23, "H 35M 11S"),
csUriStem = "/ClaraPortal",
timeTaken = 7375,
a_column_you_dont_need = "a",
another_one = "b"
)
iislog <-
raw_data %>%
mutate(
iisdate = ymd(iisdate),
iistime = hms(iistime),
csUriStem = tolower(csUriStem)
) %>%
select(iisdate:timeTaken) %>%
filter(iistime > hms("04:40:59"), iistime < hms("21:38:37"))
我在这里尝试了多种可能的解决方案,但仍然无法根据时间过滤一列。我使用 lubridate 将列转换为 hms。请指教。 R 的新手,如果这看起来多余,请见谅。
我试过过滤命令,但似乎不起作用,因为数据类型不对。性格对双。我认为 double 出现是因为 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"
以下是获得正确格式所需的内容:
iislog1$dtime <- with( iislog1, strptime(paste( iisdate,iistime),
format="%Y-%m-%d %HH %MM %SS"))
我认为您的输入格式与 hms
函数所接受的任何典型协议都不够接近。 Base R 更 "complete".
然后使用适当的"datetime"值进行比较。或者,如果您想要一个时间范围而不考虑日期,请只使用 format
到 return 时间并进行 alpha 比较。在您的实例中,'21:38:37' 与 '4:40:59' 的 alpha 比较将显示前者是 "less than" 后者,因为前导字母是“4”和“2”。正确构造的 R 日期时间中“4:40:59”的格式化版本为“04:40:59”。
你只是没有传递要过滤的数据框,你传递了一个向量。注意区别
# failes
iislog1 <- filter(iislog1$iistime > '04:40:59' & iislog1$iistime < '21:38:37')
# works |--missing--|
iislog1 <- iislog1 %>% filter(iislog1$iistime > '04:40:59' & iislog1$iistime < '21:38:37')
tidyverse
比基础 R 更简洁。最大的节省在于管道,而不必每次都命名数据框(即 df$col
)。你 可以 但你需要先传递一些东西,例如你不能做
df %>% filter(df$col < 2)
但这本身
filter(df$col < 2)
这是因为所有 dplyr
动词都希望第一个参数是要传递的内容,然后是 return 数据框。这三件事是一样的
filter(df, col < 2)
df %>% filter(., col < 2)
df %>% filter(col < 2)
所以 filter()
需要一个数据帧,而您向它传递了一个向量 df$col
,它不知道该怎么做。我希望这个解释是有道理的。 R for Data Science 这本书是一个很好的资源,而且是免费的。
总而言之,完成所有步骤的最佳方式如下
library(tidyverse)
library(lubridate)
raw_data <-
tibble(
iisdate = "2019-05-10",
iistime = paste0(1:23, "H 35M 11S"),
csUriStem = "/ClaraPortal",
timeTaken = 7375,
a_column_you_dont_need = "a",
another_one = "b"
)
iislog <-
raw_data %>%
mutate(
iisdate = ymd(iisdate),
iistime = hms(iistime),
csUriStem = tolower(csUriStem)
) %>%
select(iisdate:timeTaken) %>%
filter(iistime > hms("04:40:59"), iistime < hms("21:38:37"))