Filtering a data frame for multiple conditions

I am trying to extract lobster data from the following data frame:

dput(head(biol,10))

I want only samples that include males (i.e. sex = 1) with tail widths equal to or greater than 52cm and from the JunAug column where values are ‘JunAug’ (rather than ‘other’). I have tried this code

TLmales52 [biol$tailwidth >= '52'  & JunAug == "JunAug" & sex == "1",]%>%
  droplevels()
TLmales52

but get the following error: Error in charToDate(x) :
character string is not in a standard unambiguous format. I’m not surer what to do here. I also tried the following:

TLmales52 <- biol %>%
  select(tailwidth,sex,JunAug) %>%
  filter(biol, tailwidth >= '52' & JunAug == "JunAug" & sex == "1")%>%     droplevels()
TLmales52

but this is also erroneous i.e.

Warning: Returning data frames from filter() expressions was deprecated in dplyr 1.0.8.
Please use if_any() or if_all() instead.Error in filter():
ℹ In argument: biol.
Caused by error:
! ..1$id must be a logical vector, not an integer vector.
Backtrace:

  1. … %>% droplevels()
  2. dplyr:::dplyr_internal_error(…)
    Error in filter(., biol, tailwidth >= “52” & JunAug == “JunAug” & sex == :

Caused by error:
! ..1$id must be a logical vector, not an integer vector.

Any help would be greatly appreciated.

  • 5

    For us to have access to your data, you need to post the output of dput, not just the command

    – 

  • 2

    Remove biol from your filter, it’s extraneous, the frame is being passed automatically with the %>%. Change instead to biol %>% select(...) %>% filter(tailwidth >= '52' & ...).

    – 

  • 4

    Also be cautious about using string literals numerically ie tailwidth >= '52'. Should that be '52' or just 52? note that with '52' it is not a guarantee that tail width will be greater than 52cm. This is because a value like '6' is greater than '52' but 6 is less than 52

    – 

Leave a Comment