Summarizing a factor

After finishing this course, one of your favorite functions in R will be summary(). This will give you a quick overview of the contents of a variable:

summary(my_var)

Going back to our survey, you would like to know how many "Male" responses you have in your study, and how many "Female" responses. The summary() function gives you the answer to this question.

Instruction

Ask a summary() of the survey_vector and factor_survey_vector. Interpret the results of both vectors. Are they both equally useful in this case?

# Build factor_survey_vector with clean levels survey_vector <- c("M", "F", "F", "M", "M") factor_survey_vector <- factor(survey_vector) levels(factor_survey_vector) <- c("Female", "Male") factor_survey_vector # Generate summary for survey_vector # Generate summary for factor_survey_vector # Build factor_survey_vector with clean levels survey_vector <- c("M", "F", "F", "M", "M") factor_survey_vector <- factor(survey_vector) levels(factor_survey_vector) <- c("Female", "Male") factor_survey_vector # Generate summary for survey_vector summary(survey_vector) # Generate summary for factor_survey_vector summary(factor_survey_vector) msg = "Do not change anything about the first few lines that define `survey_vector` and `factor_survey_vector`." test_object("survey_vector", undefined_msg = msg, incorrect_msg = msg) test_object("factor_survey_vector", eq_condition = "equal", undefined_msg = msg, incorrect_msg = msg) msg <- "Have you correctly used `summary()` to generate a summary for `%s`?" test_output_contains("summary(survey_vector)", incorrect_msg = sprintf(msg, "survey_vector")) test_output_contains("summary(factor_survey_vector)", incorrect_msg = sprintf(msg, "factor_survey_vector")) success_msg("Nice! Have a look at the output. The fact that you identified `\"Male\"` and `\"Female\"` as factor levels in `factor_survey_vector` enables R to show the number of elements for each category.")

Call the summary() function on both survey_vector and factor_survey_vector, it's as simple as that!

Previous: 4-4 | Factor levels

Next: 4-6 | Battle of the sexes

Back to Main