3.1 Subsetting your dataset
WVS and States
If you are working with WVS or States data, you have datasets with a lot of variables. The WVS has 487 variables. The States dataset I downloaded includes the core data and, two policy sections add up to 649 variables. The computer can handle datasets this big, but they are unwieldy. Let’s get a subset that includes the variables we for now. We can add to this dataset later if needed.
To subset a dataset, we’ll use the select function in the dplyr package. dplyr is part of the tidyverse.
Here is a short video (about 3:00) on using the select done by an R user group, R-Ladies Sydney:
Let’s look at an example from the WVS. I have been using Ethiopia. For now, my research question is: Does where a person lives have an impact on Ethiopian’s views on democracy? Looking at the codebook, I see a series of variables on where folks reside and a series of questions on democracy. So I am going to select only the variables I want to look at for now. We can always add to or subtract from this list later.
library(tidyverse)
ethiopia_small <- ethiopia %>%
select(N_REGION_WVS, N_TOWN, G_TOWNSIZE, G_TOWNSIZE2, H_SETTLEMENT, H_URBRURAL, Q34, Q241, Q242, Q243, Q244, Q245, Q246, Q247, Q248, Q249, Q250, Q251, Q252, Q253, Q254)
A lot of typing! Since these variables are in two groups that are consecutive in the dataset, I can do this by using a shortcut :. This shortcut selects all the variables in a range of consecutive variables. There are a bunch of shortcuts, helpers for the select function.
library(tidyverse)
ethiopia_small <- ethiopia %>%
select(N_REGION_WVS : H_URBRURAL, Q34, Q241 : Q261, E1_LITERACY, Q275R)
Regardless of the dataset, this process is the same. We will look at a States example in the next section.
This video briefly shows you what I did above.
