I have the above data frame, my "user_created_at_year" column is a character column. My question is, how can I filter the entire table based on year values in rStudio? For example from "2010" to "2021"
CodePudding user response:
we can convert to numeric with as.numeric
and then filter with range :
library(dplyr)
df %>% mutate(user_created_at_year = as.numeric(user_created_at_year)) %>%
filter(user_created_at_year %in% 2010:2021)
CodePudding user response:
If the year
data are not yet ordered you will need to use, in dplyr
, the function arrange
while at the same time converting to numeric, then you can filter for a certain year range:
library(dplyr)
df %>%
arrange(as.numeric(year)) %>%
filter(year >=2010 & year <= 2021)
year
1 2011
2 2021
Data:
df <- data.frame(
year = c("2021", "2000", "2011", "2005")
)