jeudi 29 octobre 2020

How to properly filter a df based on a condition in R?

I am trying to sample a dataset based on date class column, quarterly for "Active" and monthly for "Inactive"

Here's my code:

library(dplyr)
library(lubridate)
  
## data ##
                 
df <- structure(list( 
             mes = c("01/01/2000", "01/02/2000", "01/03/2000", 
"01/04/2000", "01/05/2000", "01/06/2000", "01/07/2000", "01/08/2000", 
"01/09/2000", "01/10/2000", "01/11/2000", "01/12/2000"),
              status = c("Active", "Inactive",
                         "Active", "Inactive",
                         "Active", "Inactive",
                         "Active", "Active",
                         "Inactive", "Active",
                         "Inactive", "Active")),
             class = "data.frame",
             row.names = c(NA, -12L))

## setting date class for "mes" column ##

df$mes <- as.Date(df$mes,
                  format = "%d/%m/%Y")

## sampling ##

sample_df <- df %>%  
  dplyr :: filter(status %in% "Active",
                  status %in% "Inactive") %>%
            dplyr :: filter_if(status == "Active",
            month(mes) %in% c(3,6,9,12),
            month(mes) %in% c(1,2,3,4,5,6,7,8,9,10,11,12))

Console output:

Error in is_logical(.p) : objeto 'status' no encontrado

Is there any other library that I could use to accomplish this task?

Aucun commentaire:

Enregistrer un commentaire