Home > other >  Filter POSIXct date times for certain months
Filter POSIXct date times for certain months

Time:11-19

I have a data set including POSIXct date time stamps ($acquisition_time). I need to filter all rows of this data set that have a date time stamp in June, July, August or September.

This is a sample of my data:

> data
   animals_id    acquisition_time longitude latitude      projection collar_type
1           1 2010-01-05 19:59:00  7.611712 47.94893 EPSG:4326-WGS48         gps
2           1 2010-02-06 02:59:00  7.611367 47.95333 EPSG:4326-WGS48         gps
3           1 2010-03-06 23:59:00  7.612298 47.95245 EPSG:4326-WGS48         gps
4           1 2010-03-07 20:59:00  7.621620 47.95849 EPSG:4326-WGS48         gps
5           1 2010-04-08 17:59:00  7.611142 47.95456 EPSG:4326-WGS48         gps
6           1 2010-04-09 00:59:00  7.619372 47.95881 EPSG:4326-WGS48         gps
7           1 2010-05-09 07:59:00  7.612473 47.95379 EPSG:4326-WGS48         gps
8           1 2010-06-10 04:59:00  7.613174 47.95429 EPSG:4326-WGS48         gps
9           1 2010-06-11 22:59:00  7.612589 47.95584 EPSG:4326-WGS48         gps
10          1 2010-07-12 19:59:00  7.613384 47.95734 EPSG:4326-WGS48         gps
11          1 2010-08-13 16:59:00  7.612884 47.95448 EPSG:4326-WGS48         gps
12          1 2010-08-13 23:59:00  7.614389 47.95932 EPSG:4326-WGS48         gps
13          1 2010-08-14 20:59:00  7.617362 47.96213 EPSG:4326-WGS48         gps
14          1 2010-09-15 03:59:00  7.612436 47.95579 EPSG:4326-WGS48         gps
15          1 2010-09-15 17:59:00  7.616448 47.95875 EPSG:4326-WGS48         gps
16          1 2010-09-16 01:00:00  7.611193 47.95464 EPSG:4326-WGS48         gps
17          1 2010-10-16 21:59:00  7.619343 47.96087 EPSG:4326-WGS48         gps
18          1 2010-10-18 01:59:00  7.619420 47.95877 EPSG:4326-WGS48         gps
19          1 2010-11-18 22:59:00  7.624575 47.95586 EPSG:4326-WGS48         gps
20          1 2010-12-19 12:59:00  7.615908 47.95812 EPSG:4326-WGS48         gps
21          1 2010-01-20 23:59:00  7.605586 47.93908 EPSG:4326-WGS48         gps
22          1 2010-02-21 20:59:00  7.627373 47.96214 EPSG:4326-WGS48         gps
23          1 2010-02-22 03:59:00  7.625065 47.95793 EPSG:4326-WGS48         gps
24          1 2010-02-22 17:59:00  7.614603 47.95174 EPSG:4326-WGS48         gps
25          1 2010-02-23 07:59:00  7.613502 47.95427 EPSG:4326-WGS48         gps
   study_area_id animals_age_class animals_sex
1             13                 s           f
2             13                 s           f
3             13                 s           f
4             13                 s           f
5             13                 s           f
6             13                 s           f
7             13                 s           f
8             13                 s           f
9             13                 s           f
10            13                 s           f
11            13                 s           f
12            13                 s           f
13            13                 s           f
14            13                 s           f
15            13                 s           f
16            13                 s           f
17            13                 s           f
18            13                 s           f
19            13                 s           f
20            13                 s           f
21            13                 s           f
22            13                 s           f
23            13                 s           f
24            13                 s           f
25            13                 s           f

I tried the following code but I get an error:

data <- data$acquisition_time %>% filter(month(Year) %in% c(6,7,8,9))

Error in UseMethod("filter") : 
  no applicable method for 'filter' applied to an object of class "c('POSIXct', 'POSIXt')"

How can I do this?

CodePudding user response:

Your code is mostly correct, however, you are attempting to apply filter to a vector rather than to a data frame. The correct code would be (assuming lubridate has been loaded:

data %>%
  filter(month(acquisition_time) %in% c(6, 7, 8, 9))
  • Related