为第一个值返回R中的行号,其中包含一整小时



对于我正在运行的分析,我想知道数据集中的第一行,其中列"时间"是整整一个小时。例如,我有:

df <- data.frame(x = c("14:56:00","14:57:00","14:58:00","14:59:00","15:00:00","15:01:00"))

我想找到行号,其中x是一整小时,在这种情况下是15:00:00。

现在更好的是,如果它只返回第一个发生这种情况的情况,但我想我可以找到解决这个问题的方法。我认为grepl可能是一种方法,但我认为这对时间函数来说不是很好?

我们可以使用grep

grep(":00:00", df$x)[1]
#[1] 5

或者转换为日期-时间类,然后进行评估

v1 <- strptime(df$x, "%H:%M:%S")
which.max(v1$sec==0 & v1 $min == 0)
#[1] 5

不是一个答案,但对于注释来说太长了。我对此太好奇了,并对所讨论的各种选项进行了计时。好消息!当您使用POSIXltPOSIXcttimes对象时,有一种有效的方法可以做到这一点。

library(chron)
library(microbenchmark)
library(lubridate)
x <- c("14:56:00",
"14:57:00",
"14:58:00",
"14:59:00",
"15:00:00",
"15:01:00")
x <- x[sample(seq_along(x), 10000, replace = TRUE)]
x_times <- times(x)
x_lt <- strptime(x, "%H:%M:%S")
x_ct <- as.POSIXct(x, format = "%H:%M:%S")
microbenchmark(
grep = grep(":00:00", x),
substr = which(substr(x, 3, 8) == ":00:00"),
posixlt = which(x_lt$min == 0 & x_lt$sec == 0),
posixct = which(as.numeric(x_ct) %% 3600 == 0),
chron_times = which(as.numeric(x_times) %% (1/24) == 0),
lubridate_lt = which(minute(x_lt) == 0 & second(x_lt) == 0),
lubridate_ct = which(minute(x_ct) == 0 & second(x_ct) == 0)
)
Unit: microseconds
expr      min        lq      mean    median        uq       max neval cld
grep 1874.412 1882.4765 1900.3438 1887.9015 1897.8715  2090.239   100  b 
substr  737.508  743.9590  780.8664  745.7180  748.3570  1843.328   100 a  
posixlt  266.851  270.0780  295.3843  272.4230  275.0630  1436.600   100 a  
posixct  244.272  248.0840  268.9425  249.5500  252.3365  1365.341   100 a  
chron_times  244.272  249.4040  269.2798  251.7495  256.7345  1078.257   100 a  
lubridate_lt  286.206  290.6045  315.7149  294.8565  300.5750  1415.487   100 a  
lubridate_ct 3100.169 3128.1750 4017.1479 3166.1495 4038.4020 50903.542   100   c

包括可变强制时间

microbenchmark(
grep = grep(":00:00", x),
substr = which(substr(x, 3, 8) == ":00:00"),
posixlt = {
x_lt <- strptime(x, "%H:%M:%S")
which(x_lt$min == 0 & x_lt$sec == 0)
},
posixct = {
x_ct <- as.POSIXct(x, format = "%H:%M:%S")
which(as.numeric(x_ct) %% 3600 == 0)
},
chron_times = {
x_times <- times(x)
which(as.numeric(x_times) %% (1/24) == 0)
},
lubridate_lt = {
x_lt <- strptime(x, "%H:%M:%S")
which(minute(x_lt) == 0 & second(x_lt) == 0)
},
lubridate_ct = {
x_ct <- as.POSIXct(x, format = "%H:%M:%S")
which(minute(x_ct) == 0 & second(x_ct) == 0)
}
)
Unit: microseconds
expr        min         lq        mean     median          uq        max neval   cld
grep   1877.931   1894.353   1908.4255   1898.604   1905.2030   2109.300   100 a    
substr    739.853    748.651    796.1005    750.704    754.0755   2083.788   100 a    
posixlt  64857.238  65131.127  66115.4777  65293.731  66145.3095 113194.606   100   c  
posixct 127864.696 128861.283 130728.4082 129356.279 130059.3290 191846.601   100    d 
chron_times   8319.317   9237.315  10899.4629   9526.746   9779.0825  59084.740   100  b   
lubridate_lt  64874.832  65149.895  65733.2789  65315.431  65820.5420  79871.888   100   c  
lubridate_ct 131143.743 132104.263 133843.4980 132782.388 133162.1380 182147.867   100     e

最新更新