R Tutorial: Introduction to qualitative data

Emily Robinson teaches how to work with qualitative data in R, covering categorical and ordinal variables. Learn when to use characters versus factors, how to inspect variable types, and how to rename and reorder factor levels using the forecast package. Build visualizations with tidyverse tools to recreate a FiveThirtyEight graphic.
Chapters
Hi, my name is Emily Robinson and welcome to this course where we'll learn how to effectively wrangle and plot non numerical or qualitative data. We'll start with learning about how to identify and inspect these variables in a data set. Then we'll move to using the forecast package by Hadley Wickham to manipulate the variables by renaming categories, changing their order, and collapsing multiple groups into one. In the third chapter, we'll see how we can make effective visualizations by combining forecasts with other tidyverse packages like dplyr, tidyr, stringr, and ggplot2. In our final chapter, we'll recreate this visualization from the 538blog using all the tools we have learned. We've accessed this data from the538r package which which provides access to the code and data sets published by FiveThirtyEight. This course focuses on two types of qualitative categorical or nominal data and ordinal data. Categorical data are data that fall into unordered groups, while ordinal data have an inherent order but not a constant distance between them. Both types of data have a fixed and known set of possible values. One example of categorical data is a person's occupation. You might have a survey that has people pick their occupation from a list of 30, such as doctor, teacher, or engineer, with an extra category for other we could think of ways to order this data, such as by median salary or years of education needed, but they don't have an inherent order. On the other hand, if we asked people about their annual income and offered four choices zero to $50,000, $50,000 to $150,000, $150,000 to $500,000, and more than $500,000. This would be an ordinal variable because these groups go from smallest to largest. However, there's not a constant distance between each group. If you were asked to construct a mean salary from this data, you couldn't do it. This is what makes it qualitative instead of quantitative data. R has two ways to represent qualitative as characters and as factors. There are some differences under the hood, but generally you'll use factors for categorical and ordinal variables and characters otherwise. For example, names would best be represented as characters because there's no limit to the possible number of names. On the other hand, a survey question where you can select which programming languages you know among 40 possible answers can be represented as a factor. So how do you know whether you're variable is currently stored as a factor or character. There are two main methods. First, you can look at the printed output of your tibble. If you store your data set as a tibble, a modern data frame, each column type is automatically printed out below or next to the column name. Let's print out the College all ages Data set. Another one from the 538 package. We can see that there aren't any factors. Major and minor category are both character columns. The second way is to use the function is factor. This will be true or false depending on whether you guessed it, the input is a factor. When we check the major category column, we see just as above, it's not a factor. Now that we've been introduced to qualitative variables in R, let's try working with some examples.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







