Stringr Continued
- Refer Here for the changes done where we used replace and split methods of string
Factors with forcats
- In R, factors are used to work with categorical variables.
- Prereq’s
library(tidyverse)
install.packages(forcats)
library(forcats)
- sorting ordinal categorical variables
- Parsing vectors into factors in the case of invalid values
- For the next set of examples lets use General Social Survey Dataset
forcats::gss_cat
View(forcats::gss_cat)
glimpse(forcats::gss_cat)
- Lets count the different races in gss_cat
- Summary by religion
- Lets summarize by income
-
Lets summarize by age
-
Explore the below R statements
gss_cat %>%
mutate(marital = marital %>% fct_infreq() %>% fct_rev()) %>%
ggplot(aes(marital)) +
geom_bar()
gss_cat %>%
mutate(marital = marital) %>%
ggplot(aes(marital)) +
geom_bar()
- Recoding factors
- Collapsing multiple categories into one