Search code examples
rdelete-rowthreshold

Removing rows of data in R below a specified value


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.


Solution

  • Use the lubridate package to transform your string time column into some kind of time class:

    library(lubridate) 
    sample$GPS.Time <- hms(sample$GPS.Time)
    

    To achieve the required output, just use subsetting with brackets ([), with the condition you want. In your example, I removed all rows up to 11:39:42.

    output <- sample[sample$GPS.Time < hms("11:39:42"),]