Look at the Data SOCL 3633 Research Methods · Lab 2 · Midwestern State University
Today you open the General Social Survey data, find out what is in it, and find out what is missing from it. Last week the script was complete. This week you fill in blanks and run the lines yourself, one at a time. Keep this page open next to RStudio and work through the tabs in order. Before you leave, you submit two things in the Lab 2 submission on D2L: your response sheet and the picture file of your own chart.
Get set up
Check off each step as you finish it.
Two new things today
____. Delete the four underscores and type the word the comment above the line asks for. A line with a blank still in it will not run.library(dplyr) that says "The following objects are masked" is a normal message, not an error.Fill in and run: Steps 2 to 5
These steps answer three questions about the data: how many people are in it, what was recorded about each person, and how many people were surveyed in each year.
Step 2: how big is the data?
- Fill in both blanks with
gss:nrow(gss) ncol(gss)
- Run each line with Ctrl+Enter.
[1] 21483 and then [1] 9. The data has 21,483 rows and 9 columns. Each row stands for one person who answered the survey. Each column is one variable: one survey question, or one fact recorded about the person.Step 3: what is in each column?
Run glimpse(gss). Each line of the output is one variable, with its type and its first few values. In the tvhours line, some values are NA. You will come back to those in tab 3.
Step 4: how many people in each year?
- Fill in the blank with
yearand run the line:count(gss, year)
- Read the eight counts. One year has far more people than the others.
Step 5: keep only one year
- Fill in the blank with
2014. Numbers do not need quotation marks.gss_2014 <- filter(gss, year == 2014)
- Run it. Nothing prints. R stored the result under the name
gss_2014, which now appears in the Environment pane. - Fill in the next blank with
gss_2014and runnrow(gss_2014). Then run thecount(gss_2014, marital)line.
nrow(gss_2014) gives 2538, the same number Step 4 showed for 2014. The filter() function keeps the rows that match a rule. The double equals sign == asks "is this equal to?" A single = does something else in R, so a rule always uses two.If something goes wrong
| What you see | What to do |
|---|---|
| "unexpected input" or "unexpected symbol" | A blank is still in the line, or part of it is. Delete all four underscores and type the word. |
| "object 'gss' not found" | Step 1 did not run in this session. Run the lines in Step 1 again, then come back. |
| "could not find function" | The library() lines did not run. Run Step 1 again. |
| "object 'Year' not found" or a similar name | R is case-sensitive. Variable names in this data are all lower case. |
Error that mentions = inside filter() | Use two equals signs: year == 2014. |
A + sign in the Console and nothing happens | A parenthesis or quotation mark is missing. Click in the Console, press Escape, fix the line, and run it again. |
What the counts leave out: Steps 6 to 8
Every dataset has people with no answer recorded for some questions. Before you report anything from a variable, you need to know who has no answer and why. These steps look at two variables where the answer is "many people."
Step 6: the income categories
- Fill in the blank with
rincomeand run the line:count(gss, rincome)
- Read all 16 categories. Most are amounts of money. ("Lt $1000" means less than $1,000.) Four categories are not amounts of money: the first three and the last one.
rincome is income from the person's own work.Step 7: who has no TV hours?
- Run
count(gss, is.na(tvhours)). Theis.na()function asks "is this value missing?"TRUEmeans missing.FALSEmeans a value is there. - Fill in the blank in the next line with
2004. Run that line and thecount()line below it. - Change
2004to2014. Run both lines again. Compare the two years.
Step 8: chart the income categories
- Fill in the blank with
rincome. - Select every line from
income_chart <-down totheme_minimal(base_size = 14), and press Ctrl+Enter. Then runprint(income_chart).
On your own: Step 9
You choose the year and the variable, write the title, and write the line that saves the chart.
- Pick a year. In the first blank, put any survey year from Step 4.
- Pick a variable. In the second blank, put one of these:
marital,race,partyid, orrelig. - Write a title. Replace "Write your title here" with a complete sentence that says what the chart shows. Keep it to about ten words so it fits, and keep the quotation marks.
- Select every line from
my_year <-down toprint(my_chart)and press Ctrl+Enter. - Save the chart. Under "Write your ggsave() line here," write one line that saves
my_chart, as you did in Lab 1. Name the file with your last name, for examplesmith_lab2.png. Run the line. The file appears in the Files pane.
partyid in 2014. Yours will use your own year, variable, and title. Look at the "No answer" and "Don't know" bars in your chart. They are part of what the counts leave out.Then: find a GSS question for your own topic
Question 4 of the response sheet asks you to find one GSS question related to your Writing assignment 1 topic. Use the GSS Data Explorer at gssdataexplorer.norc.org. You do not need an account to search.
- Click Search Variables.
- Type a keyword from your topic, such as work schedule, church, neighbors, internet, or health.
- Open one result. Write down the variable name (a short code in capital letters, such as TVHOURS) and the exact wording of the question.
Lab 2 response sheet
Answer the four questions in complete sentences. Then assemble your answers into one block of text and paste it into the Lab 2 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.
Sources
NORC at the University of Chicago. n.d. "Frequently Asked Questions." General Social Survey. https://gss.norc.org/faq.html (source for the statement that since 1988 many GSS questions have been asked of a random two-thirds of each sample)
NORC at the University of Chicago. n.d. GSS Data Explorer. https://gssdataexplorer.norc.org
R Core Team. 2024. R: A Language and Environment for Statistical Computing. Vienna: R Foundation for Statistical Computing. https://www.R-project.org
Wickham, Hadley. 2016. ggplot2: Elegant Graphics for Data Analysis. New York: Springer.
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.