Grouping Categories SOCL 3633 Research Methods · Lab 4 · Midwestern State University

Researchers often combine the categories of a variable into a few groups. That is a measurement decision, and it can change the answer. Today you group the same party answers in two ways, group the religion answers your own way, and read four real GSS questions closely. Keep this page open next to RStudio and work through the tabs in order. Before you leave, you submit two things in the Lab 4 submission on D2L: your response sheet and the picture file of your own chart.

Get set up

One new tool today

fct_collapse()
Combines the categories of a variable into new groups. Each line inside it reads: "new group name" = c("old category", "old category"). Anything you do not list goes into the group named by other_level.
mutate()
Adds a new column to the data. Here the new column holds the grouped version of the variable, so the original stays unchanged.

Fill in and run: Steps 2 to 4

Step 2: party identification in 2014

  1. Fill in 2014 and run the filter() line. Then fill in partyid and run the count() line.
  2. Read the ten categories. The GSS asks, "Generally speaking, do you usually think of yourself as a Republican, Democrat, Independent, or what?" People who answer Independent are asked whether they are closer to one party. Their answers become "Ind,near dem" and "Ind,near rep." People who study politics call them leaners.
Checkpoint. Independent has 502 people, Ind,near dem has 337, and Ind,near rep has 249.

Step 3: version A, leaners counted as independents

Before you run Step 3: if leaners are counted as independents, what percent of the 2,538 people in 2014 are independents?
20%
  1. Select all six lines from party_a <- to other_level = "All other answers")) and press Ctrl+Enter. Nothing prints.
  2. Run count(party_a, party3).

Step 4: version B, leaners counted with their party

  1. In the Democrat line, replace ____ with Ind,near dem, keeping the quotation marks around it. In the Republican line, replace ____ with Ind,near rep.
  2. Select all six lines from party_b <- and run them. Then run count(party_b, party3).
Checkpoint. Version A: Democrat 825, Independent 1,088, Republican 537. Version B: Democrat 1,162, Independent 502, Republican 786. In both versions, 88 people are in "All other answers."

If something goes wrong

What you seeWhat to do
A warning that says "Unknown levels"A category name is misspelled. Copy it exactly from the Step 2 output, including the comma and lower-case letters: Ind,near dem.
Version B shows 674 people in "All other answers"The two blanks are still empty or misspelled, so the leaners went into "All other answers." Fix the names and run the six lines again.
"unexpected symbol"A quotation mark or comma is missing. Each category is in quotation marks, with a comma between categories.
A + sign in the Console and nothing happensA parenthesis is missing. Press Escape in the Console, fix the line, and run all six lines again.
"object 'gss_2014' not found"Step 2 did not run. Run it, then come back.

Compare the versions: Step 5

Step 5 has no blanks. Select every line from both <- down to print(compare_chart) and run them together. Your chart should look like the one below.

Two horizontal bar charts, one above the other. Version A, with leaners counted as independents: Independent about 1,090, Democrat about 830, Republican about 540, all other answers about 90. Version B, with leaners counted with their party: Democrat about 1,160, Republican about 790, Independent about 500, all other answers about 90.
The 2,538 answers are identical in both panels. Only the grouping changed. In version A, independents are the largest group. In version B, they are the smallest of the three. A news story could report either one. Question 1 and question 2 of the response sheet ask about this chart.

On your own: Steps 6 and 7

  1. Run count(gss_2014, relig, sort = TRUE) to see all the religion categories in 2014.
  2. Change the grouping. The script starts with two groups, Christian and No religion, plus "All other answers." Change it to a grouping you think is better for some research question. Make at least one change: for example, give Catholic its own group, or give Jewish its own group, or move "Christian" (people who answered only "Christian") to a different group. Type category names exactly as they appear in the output, including old spellings such as Moslem/islam.
  3. Select all the lines from my_groups <- and run them. Then run count(my_groups, relig_groups).
  4. Write a title in Step 7: a complete sentence of about ten words that says what your chart shows. Run all the chart lines, then print(my_chart).
  5. Save the chart. Write one ggsave() line that saves my_chart with your last name in the file name, for example smith_lab4.png. Run it.
Here is one example, with Catholic as its own group. Yours will use your own grouping and title. Look at what ends up in "All other answers": people of several religions, plus people who said "Don't know" or gave no answer. Question 3 asks what your grouping hides.
Example horizontal bar chart titled Half of respondents were Protestant or other Christian. Protestant or other Christian about 1,270, Catholic about 610, No religion about 520, All other answers about 140.
Before you leave: save the script in your SOCL3633 folder (File, then Save).

Four GSS questions

Every variable starts as a question someone wrote. Here are four GSS questions, worded as the GSS asks them. For each one, choose the concept it measures best. The reference call and a note on the wording appear after you choose. Question 4 on the response sheet asks you to pick one of these.

0 of 4 questions judged.

Lab 4 response sheet

Answer in complete sentences. Then assemble your answers and paste them into the Lab 4 submission in D2L. In the same submission, upload the picture file of your own chart. Your answers stay on this page and are not sent anywhere until you paste them.

Your name
1. Under version A and version B, what share of 2014 respondents are independents? Why do the two numbers differ?
Two to three sentences. Use the counts and the total of 2,538.
2. Which version would you use to answer the question "Are more Americans independent of the two parties?" Defend your choice.
Three to four sentences.
3. Describe your own grouping of the religion variable: which groups you made, and what your choice hides.
Two to three sentences.
4. Pick one of the four GSS questions from tab 5. Name the concept it measures and one group of people for whom the question works badly.
Two to three sentences. Say why it works badly for that group.

Sources

NORC at the University of Chicago. General Social Survey, 2014. Accessed through the gss_cat extract in the forcats package for R. https://gss.norc.org
NORC at the University of Chicago. General Social Survey codebook: question wording for PARTYID, HAPPY, TRUST, ATTEND, and WRKSTAT. https://gss.norc.org (punctuation lightly edited)
Pew Research Center. 2019. "Political Independents: Who They Are, What They Think." March 14. https://www.pewresearch.org/politics/2019/03/14/political-independents-who-they-are-what-they-think/
Wickham, Hadley. 2023. forcats: Tools for Working with Categorical Variables (Factors). R package version 1.0.0. https://forcats.tidyverse.org
Wickham, Hadley, Romain François, Lionel Henry, Kirill Müller, and Davis Vaughan. 2023. dplyr: A Grammar of Data Manipulation. R package. https://dplyr.tidyverse.org

All counts on this page come from the gss_cat extract and were checked by running the lab script. They are unweighted.