Write an R script to define a new character vector gender that is populated with “male” for the first 20 elements, and “female” for the next 30 elements. Use factors to quickly and efficiently calculate a total number of values in each gender category.
I created a dataset, but how I can use factor to calculate the number of elements?
I know only how to use table in this situation but I need a factor.
male <- c(length = 20)
male[1:20] <- "male"
female <-c(length=30)
female[1:30] <- "female"
gender=c(male,female)
factor(gender)
#out
#length
#male male male male male male male male male male male male male male
#length
#male male male male male male female female female female female female female female
#
#female female female female female female female female female female female female female female
#
#female female female female female female female female
#Levels: female male