Filtering the same year in different columns

Hello All,

I am in need of your help.

I have a scenario. I have two columns, one titled HIV_Date and the other x1st_service

the dataset I am using is just called Patient_Demographics

what I want to do is in each column (HIV_Date & x1st_service) I want to filter out the year 2021 so just 2021 cases show.

I used the coding below, but I got back 0 rows

Demographic.Data %>%
filter(HIV.Date == 2021 | X1st.Service == 2021)

0 rows | 1-8 of 17 columns = output

What would the proper method be to be able to filter out the data set by this ask? I have not converted the dates by using MDY or as.date either.

All the help is greatly appreciated.

Thank you!

Hi,

In order for us to help you, it's easier if you create a reprex which we can work with. A reprex consists of the minimal code and data needed to recreate the issue/question you're having. You can find instructions how to build and share one here:

Normally the filtering you wrote should work like so:

library(dplyr)

set.seed(2)
myData = data.frame(
  x = runif(10),
  HIV.Date = sample(c(2020,2021), 10, replace = T),
  X1st.Service = sample(c(2020,2021), 10, replace = T)
)

myData %>% filter(HIV.Date == 2021 | X1st.Service == 2021)
#>           x HIV.Date X1st.Service
#> 1 0.5733263     2021         2020
#> 2 0.1680519     2020         2021
#> 3 0.1291590     2021         2021
#> 4 0.8334488     2021         2020
#> 5 0.4680185     2021         2021
#> 6 0.5499837     2021         2021

Created on 2021-07-13 by the reprex package (v2.0.0)

Post a reprex and we can take it from there
PJ

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.