带时间戳的筛选器不筛选数据R
我有一个大数据集(67000 obs,6个变量),我正试图使用相关的时间戳对其进行过滤。我使用的是带时间戳的筛选器不筛选数据R,r,R,我有一个大数据集(67000 obs,6个变量),我正试图使用相关的时间戳对其进行过滤。我使用的是dplyr::filter函数,当从数据集中删除一些行时,它的行为与我预期的不一样。见下文: 示例数据 timestamp Var2 Var3 12.58.00 0.0 1.2 12.58.10 0.1 1.5 12.58.20 0.2 1.3 ... 2.49.50 6719.79 1.37 2.4
dplyr::filter
函数,当从数据集中删除一些行时,它的行为与我预期的不一样。见下文:
示例数据
timestamp Var2 Var3
12.58.00 0.0 1.2
12.58.10 0.1 1.5
12.58.20 0.2 1.3
...
2.49.50 6719.79 1.37
2.49.60 6719.89 1.20
2.49.70 6719.99 1.14
带有过滤器的脚本
调用:
data <- read_excel("file.xlsx", col_names = TRUE)
data$timestamp <- sapply(strsplit(data$timestamp, split = " ", fixed = TRUE), function(x) (x[2]))
data$timestamp <- str_replace_all(data$timestamp, ":", ".")
data <- filter(data, data$timestamp > "1.29.00" & data$timestamp < "2.51.00")
我得到的不是预期结果,而是一个数据帧,其中包含带有时间戳的行:
12.58.00-12.59.59
那么下一行是:
1.29.11
我调用的
过滤器中有什么东西没有按我认为的那样工作?非常感谢。您正在比较字符串。要比较时间,请将时间戳转换为POSIXct或类似格式
df$t1 <- as.POSIXct(df$timestamp, format = '%H.%M.%S')
start <- as.POSIXct("1.29.00", format = '%H.%M.%S')
end <- as.POSIXct("2.51.00", format = '%H.%M.%S')
subset(df, t1 > start & t1 < end)
您正在比较字符串。要比较时间,请将时间戳转换为POSIXct或类似格式
df$t1 <- as.POSIXct(df$timestamp, format = '%H.%M.%S')
start <- as.POSIXct("1.29.00", format = '%H.%M.%S')
end <- as.POSIXct("2.51.00", format = '%H.%M.%S')
subset(df, t1 > start & t1 < end)
太好了,我没有想到数据类,谢谢一堆。太好了,我没有想到数据类,非常感谢
library(dplyr)
df %>% filter(between(t1, start, end))