0

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.

Paulo MiraMor
  • 1,582
  • 12
  • 30
Student
  • 65
  • 1
  • 8
  • 3
    Please add some sample data so that we can understand your needs better and the question is more reproducible. – Paulo MiraMor Jun 30 '15 at 14:05
  • You could start by working through simple examples like `DF <- data.frame(x = 1:5); DF[ DF$x > 2 & DF$x < 5 ,]` and the `subset` command – Frank Jun 30 '15 at 14:09
  • 1
    "Reproducible" means the data can be copy-pasted from your answer (not from a comment) into R so that we're looking at the same data as you. A helpful reference: http://stackoverflow.com/a/28481250/1191259 – Frank Jun 30 '15 at 14:10
  • You can filter via df[df$date > as.Date("2015-04-01"),] – MarkeD Jun 30 '15 at 14:24
  • I have now included an example of the data within the question. – Student Jun 30 '15 at 14:25

3 Answers3

0

Turn the GPS.Time into a "POSIXct" object:

df$time <- as.POSIXct(df$GPS.Time, format="%H:%M:%S")

Then you can filter using logic:

filtered_df <- df[df$time < as.POSIXct("12:30:00", format="%H:%M:%S"), ]
MarkeD
  • 2,500
  • 2
  • 21
  • 35
0

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"),]
Paulo MiraMor
  • 1,582
  • 12
  • 30
  • Thank you, this worked on my data set. Although I had to use >= instead of < to get the desired output of keeping data above 12:30:00. – Student Jun 30 '15 at 15:55
0

You can convert the entries in the "GPS.Time" columns into characters (this is originally a factor variable). After that you can separate the set by comparing the times with a specified cutoff-time stored as a character string that should be written in the same format (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
RHertel
  • 23,412
  • 5
  • 38
  • 64