Section 2.6 Dplyr Package
We will use the
dplyr package to recode some of our variables. The dplyr package provides a set of grammars of data manipulation, offering a consistent set of verbs (functions) that help us solve common data manipulation challenges. We do not need to install this R package. Do you remember that we installed the tidyverse package in the previous chapter? The tidyverse package is a collection of several packages, including dplyr and ggplot2. So dplyr was already installed when we installed the tidyverse package.
One of the
dplyr verbs is mutate(), and this function can be used to add new variables (columns) that are functions of existing variables. First, we will recode RACE to a factor, ensuring that it is treated as a categorical variable. However, we want to create a copy of the dataset in case we make mistakes and need to restore the original file. Again, you remember that we can create a new dataset using the left-arrow operator. This new dataset will be named GSS.2012.cleaned.
GSS.2012.cleaned <- GSS.2012 %>%
mutate(RACE = as.factor(x = RACE))
The first line,
GSS.2012.cleaned <- GSS.2012 %>%, creates a new dataset called GSS.2012.cleaned. This new dataset will include all our data manipulations after the %>% operator. The %>% operator, pronounced βpipeβ, lets us string multiple functions together in a sequence. This makes the code easier to read and understand, especially when handling complex data tasks. The second line, mutate(RACE = as.factor(x = RACE)), uses the mutate() function from dplyr to modify the RACE variable in the GSS.2012 dataset. It changes the RACE variable into a factor using the as.factor() function, ensuring it is treated as a categorical variable. The x = RACE argument specifies the variable to be transformed. The first RACE in the parenthesis indicates the name of the variable in the new dataset. You can keep it as it is or create a new variable with a new name. This line of code ensures that the RACE variable is treated as a categorical factor variable in the dataset.
We can examine whether our data transformation succeeded using the
summary function we learned earlier.
summary(GSS.2012.cleaned$RACE)
We can specify the variable we want to see in the dataset using the
$ operator. You will notice that no information is presented regarding minimum, 1st quartile, median, mean, 3rd quartile, and maximum values. Instead, you can see categories 1, 2, and 3:
-
Category 1: This category has a frequency count of 1477.
-
Category 2: This category has a frequency count of 301.
-
Category 3: This category has a frequency count of 196.
However, because we do not know which racial category is associated with each category, we will need to recode the values in the
RACE variable using the following syntax:
GSS.2012.cleaned <- GSS.2012.cleaned %>%
mutate(RACE = recode(.x = RACE, "1" = "WHITE")) %>%
mutate(RACE = recode(.x = RACE, "2" = "BLACK")) %>%
mutate(RACE = recode(.x = RACE, "3" = "OTHER"))
mutate(RACE = recode(.x = RACE, "1" = "WHITE")) recoded the values in the RACE variable by replacing a value of "1" with "WHITE". Similarly, mutate(RACE = recode(.x = RACE, "2" = "BLACK")) recodes the RACE variable again. This time, it replaces any value of "2" with "BLACK". Finally, mutate(RACE = recode(.x = RACE, "3" = "OTHER")) replaced any value of "3" with "OTHER".
We can review our results using the
summary function.
summary(GSS.2012.cleaned$RACE)
The output indicated that there were 1477 whites, 301 blacks, and 196 others.
Even though we broke down this data transformation process into several steps (converting the
RACE variable to a factor and then recoding its values to more descriptive labels), you do not need to create multiple syntaxes because the pipe operator in R can simplify and streamline our codes. For instance, the following syntax can perform a series of data manipulations at the same time.
GSS.2012.cleaned <- GSS.2012 %>%
mutate(RACE = as.factor(x = RACE)) %>%
mutate(RACE = recode(.x = RACE, "1" = "WHITE")) %>%
mutate(RACE = recode(.x = RACE, "2" = "BLACK")) %>%
mutate(RACE = recode(.x = RACE, "3" = "OTHER"))
summary(GSS.2012.cleaned$RACE)
In sum, we can chain multiple actions together, making our code more concise and readable.
