1
votes

I am trying to subset a data based on <= logic using dplyr in R. Even after running filter function, the data is not being filtered.

How can I fix this?

Code

library(tidyverse)
    
value = c("a,b,c,d,e,f")
Year = c(2020,2020,2020,2020,2020,2020)
Month = c(01,01,12,12,07,07)
dummy_df = data.frame(value, Year, Month)
dummy_df = dplyr::filter(dummy_df, Month <=07)

Now on a dummy data frame this does work, but when I use this function on an actual data set with in which I created Year, Month and Day columns using lubridate; I still see data from months greater than 07.

2
If yoyu are using <= 7 what do you expectakrun
i.e. you have only two values 1 and 7, and thus less than or equal to include both 1 and 7akrun
Please note that 07 with prefix 0 is still numeric unless you have an actual string "07", then the filter is differentakrun
Yes, that was a careless mistake (thank you for pointing that out) while creating a sample data frame for the question; I have updated my question to explain what's really going on.Mandalorian

2 Answers

2
votes

It may be because the OP's original dataset may be having 'Month' as character column. Convert to numeric and it should work

dummy_df = dplyr::filter(dummy_df, as.numeric(Month) <= 7)
2
votes

Or in base R we could do:

subset(dummy_df, as.numeric(Month) <= 7)

        value Year Month
1 a,b,c,d,e,f 2020     1
2 a,b,c,d,e,f 2020     1
5 a,b,c,d,e,f 2020     7
6 a,b,c,d,e,f 2020     7