I was wondering if anybody could help...
I have a data frame which includes a continuous time column and I am trying to remove all rows below a specified time.
The data starts from approx. 11:29:00 but I want to remove all rows before the time 12:30.00 and after the time 14:20.00. Since the data is recorded every second, deleting unnecessary rows will be a great help and make managing this data a whole lot easier for me so any help would be greatly appreciated.
This is the head of the data frame, as you can see the time is continuous in seconds. I would like to remove all these rows up to 12:30:00 within the GPS.Time column. Hope that makes sense.
Raw.Vel. Smooth.Vel. GPS.Time
1.486 0.755 11:39:39
1.425 1.167 11:39:40
1.466 1.398 11:39:41
1.533 1.552 11:39:42
1.517 1.594 11:39:43
1.918 1.556 11:39:44
Creating above data frame:
Raw.Vel. <- c(1.486,1.425, 1.466, 1.533, 1.517, 1.918)
Smooth.Vel. <- c(0.755, 1.167, 1.398, 1.552, 1.594, 1.556)
GPS.Time <- c("11:39:39", "11:39:40", "11:39:41", "11:39:42", "11:39:43", "11:39:44")
sample <- data.frame(Raw.Vel., Smooth.Vel., GPS.Time)
Thanks in advance.
答案 0 :(得分:0)
将GPS.Time转换为&#34; POSIXct&#34;对象:
df$time <- as.POSIXct(df$GPS.Time, format="%H:%M:%S")
然后你可以使用逻辑过滤:
filtered_df <- df[df$time < as.POSIXct("12:30:00", format="%H:%M:%S"), ]
答案 1 :(得分:0)
使用lubridate
包将您的字符串时间列转换为某种时间类:
library(lubridate)
sample$GPS.Time <- hms(sample$GPS.Time)
要获得所需的输出,只需使用带括号([
)的子集,并满足您想要的条件。在您的示例中,我删除了所有行,直到11:39:42。
output <- sample[sample$GPS.Time < hms("11:39:42"),]
答案 2 :(得分:0)
您可以转换&#34; GPS.Time&#34;中的条目。列成字符(这最初是一个因子变量)。之后,您可以通过将时间与指定的截止时间进行比较来分离集合,该截止时间存储为应以相同格式(HH:MM:SS)写入的字符串:
sample$GPS.Time <- as.character(sample$GPS.Time)
cutoff_time <- "11:39:42" # modify as necessary
sample <- sample[-which(sample$GPS.Time < cutoff_time),] #remove all rows with times smaller than the cutoff_time
#> sample
# Raw.Vel. Smooth.Vel. GPS.Time
#4 1.533 1.552 11:39:42
#5 1.517 1.594 11:39:43
#6 1.918 1.556 11:39:44