Categorical Variable Development

set.seed(143)
# Create sampe data in R
data <- data.frame(
  ID = 1:30,
  Gender = sample(c("Male", "Female"), 30, replace = TRUE),
  AgeGroup = sample(c("18-25", "26-35", "36-45"), 30, replace = TRUE),
  EducationLevel = sample(c("High School", "Bachelor's", "Master's", "PhD"), 30, replace = TRUE),
  stringsAsFactors = FALSE  # Ensure that character data is not automatically converted to factors
)

#Structure
str(data)
## 'data.frame':    30 obs. of  4 variables:
##  $ ID            : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ Gender        : chr  "Female" "Female" "Female" "Female" ...
##  $ AgeGroup      : chr  "18-25" "36-45" "18-25" "36-45" ...
##  $ EducationLevel: chr  "Bachelor's" "Master's" "Master's" "High School" ...
#Factors
factor_gender<- factor(data$Gender)
factor_agegroup<- factor(data$AgeGroup)
factor_educationlevel<- factor(data$EducationLevel)
#Factors are preferred for categorical variables because it allows you to control information order, it enforces information validity, and they make it easier to input information into other mathematical operations. 

#Structure
str(factor_gender)
##  Factor w/ 2 levels "Female","Male": 1 1 1 1 2 1 1 2 1 2 ...
str(factor_educationlevel)
##  Factor w/ 4 levels "Bachelor's","High School",..: 1 3 3 2 4 1 1 3 1 3 ...
str(factor_agegroup)
##  Factor w/ 3 levels "18-25","26-35",..: 1 3 1 3 3 3 3 3 2 1 ...
#Summary
summary(factor_gender)
## Female   Male 
##     18     12
summary(factor_educationlevel)
##  Bachelor's High School    Master's         PhD 
##          11           5           9           5
summary(factor_agegroup)
## 18-25 26-35 36-45 
##    10     5    15
#Graph
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.6.2
ggplot(data, aes(x = AgeGroup)) +
  geom_bar(fill = "steelblue") +
  labs(
    title = "Distribution of Age Groups",
    x = "Age Group",
    y = "# of Individuals in Group"
  ) +
  theme_minimal()

#Interpretations
#Ages 36-45 are the prevalent in this dataset, with 15 individuals being in the group. Uneven age groups can cause bias results, making the data unreliable and limited. There are no surprising findings or trends because the data was generated in independent columns.