1

I have a data frame that looks like this:

Subject  Time  Freq1  Freq2 ...
  A      6:20    0.6    0.1
  A      6:30    0.1    0.5
  A      6:40    0.6    0.1
  A      6:50    0.6    0.1
  A      7:00    0.3    0.4
  A      7:10    0.1    0.5
  A      7:20    0.1    0.5
  B      6:00    ...    ...

I need to delete the rows in the time range it is not from 7:00 to 7:30.So in this case, all the 6:00, 6:10, 6:20...

I have tried creating a data frame with just the times I want to keep but I does not seem to recognize the times as a number nor as a name. And I get the same error when trying to directly remove the ones I don't need. It is probably quite simple but I haven't found any solution.

Any suggestions?

www
  • 38,575
  • 12
  • 48
  • 84
Ana
  • 23
  • 4
  • If you want help re why the "creating a data frame with just the times" approach didn't work, we'd probably need a more detailed and easily reproduced example. Some guidance here if you're interested: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/28481250#28481250 – Frank May 01 '18 at 15:37

1 Answers1

1

We can convert the time column to a Period class under the package and then filter the data frame based on that column.

library(dplyr)
library(lubridate)

dat2 <- dat %>%
  mutate(HM = hm(Time)) %>%
  filter(HM < hm("7:00") | HM > hm("7:30")) %>%
  select(-HM)
dat2
#   Subject Time Freq1 Freq2
# 1       A 6:20   0.6   0.1
# 2       A 6:30   0.1   0.5
# 3       A 6:40   0.6   0.1
# 4       A 6:50   0.6   0.1
# 5       B 6:00    NA    NA

DATA

dat <- read.table(text = "Subject  Time  Freq1  Freq2
  A      '6:20'    0.6    0.1
                  A      '6:30'    0.1    0.5
                  A      '6:40'    0.6    0.1
                  A      '6:50'    0.6    0.1
                  A      '7:00'    0.3    0.4
                  A      '7:10'    0.1    0.5
                  A      '7:20'    0.1    0.5
                  B      '6:00'    NA     NA",
                  header = TRUE)
www
  • 38,575
  • 12
  • 48
  • 84
  • The mutate function worked perfectly, thanks. Select and filter did not work, but I could manage after that. – Ana May 02 '18 at 08:55