0

I'm trying to subset the shooting season (1st Nov- 1st April) from my multi-year dataset. I've run this in r: format.Date(timestamp(GWF.df), "%d")=="01" & format.Date(timestamp(GWF.df), "%m")=="11:04")

to try to filetr out data from the months 11-04, but R gets stuck everytime and i get an error 502 message. Is there another easy way to do this?

Phil
  • 7,287
  • 3
  • 36
  • 66
  • 1
    Hi Marta. It will really help people to answer your question if you can produce a minimal, reproducible example. See [this question](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for guidance. – Captain Hat Feb 16 '22 at 11:55
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 19 '22 at 17:48

1 Answers1

0

Welcome to StackOveflow. A small reproducible example would help a lot (as Captain Hat advises).

Without being able to see your data though, i would guess that something like this will work for you:

require(dplyr)
require(lubridate)
df %>% 
    filter(!lubridate::month(as.Date(GWF)) %in% c(11, 12, 1, 2, 3, 4))

Here's an example which runs using the flights dataset:

install.packages('nycflights13')
library(nycflights13)

removing_some_months <- flights %>% 
    filter(!lubridate::month(as.Date(time_hour)) %in% c(11, 12, 1, 2, 3, 4))

removing_some_months %>% 
    group_by(month) %>% 
    count()

# A tibble: 6 x 2
# Groups:   month [6]
  month     n
  <dbl> <int>
1     5 28783
2     6 28231
3     7 29428
4     8 29381
5     9 27529
6    10 28905

C.Robin
  • 1,085
  • 1
  • 10
  • 23
  • Hi, i've tried this and I get an - 'filter' applied to an object of class "function" - error message – Marta Rabanales Feb 16 '22 at 12:48
  • Can you share the exact code you ran please? – C.Robin Feb 16 '22 at 12:57
  • require(dplyr) require(lubridate) df %>% filter(!lubridate::month(as.Date(GWF.df$timestamp)) %in% c(11, 12, 1, 2, 3, 4)) class(GWF.df$timestamp) GWF.winter<-GWF.df[GWF.df$timestamp > "2012-11-01" & GWF.df$timestamp < "2020-04-01",] subset(GWF.df, format.Date(timestamp(GWF.df), "%d%m")=="01-11") subset(GWF.df, format.Date(timestamp(GWF.df), "%d")=="01" & format.Date(timestamp(GWF.df), "%m")=="11:04") – Marta Rabanales Feb 16 '22 at 13:53
  • Ignoring all but the first bit, try this: `GWF.df %>% filter(!lubridate::month(as.Date(timestamp)) %in% c(11, 12, 1, 2, 3, 4))` – C.Robin Feb 16 '22 at 15:53