Home > Software engineering >  How to user define filtering parameters in R?
How to user define filtering parameters in R?

Time:12-16

I need some help with user defined functions or a similar method that lets me define filtering and use it repeatedly. I have a DF that i need to filter in to many smaller DF-s, Some of the filtering logic i need to reuse and change from time to time. I want to define the filtering logic so i can change it only in one place and then have it applied to all of the filtering i must do with it.

I would like to define the following filterings:

NB! I know the following is not hod to define a function. This is just to illustrate what i need.

Filter_A <- DF %>%
     filter(between(EMTAK, 200, 232) |
            between(EMTAK, 400, 450))

Filter_B <- DF %>%
     filter(between(EMTAK, 250, 260) |
            EMTAK == 500))

When i'm writing code i would like to re use the above filtering in a following way:

New_DF <- DF %>%
     filter(EMTAK == Filter_A |
            EMTAK == Filter_B,
            SECTOR == "Metal" |
            SECTOR == "Plastic")

In short i would like to define filtering and then use that filtering with other filterings to get the DF i need.

Could you please show me how do this :)

Thank you,

CodePudding user response:

Either define filter variables in your DF and (re-)use those variables in filter.

DF %>%
 mutate(
    filterA = between(EMTAK, 200, 232) |
            between(EMTAK, 400, 450),
    filterB = between(EMTAK, 250, 260) |
            EMTAK == 500)
 )

New_DF <- DF %>%
     filter(Filter_A |
            Filter_B,
            SECTOR == "Metal" |
            SECTOR == "Plastic")

Or define functions for your filters. Note that when calling the functions you don't need the == because the functions already return a logical vector.

filterA <- function(x){
  between(x, 200, 232) |
    between(x, 400, 450)
}

filterB <- function(x){
  between(x, 250, 260) |
    x == 500)
}

New_DF <- DF %>%
     filter(filterA(EMTAK) |
              filterB(EMTAK),
            SECTOR == "Metal" |
            SECTOR == "Plastic")
  • Related