R for Social Science
Preface
Welcome to R for Social Science! This book introduces R, tidystata, and the tidyverse through practical examples from social-science research. You will learn how to organize your work, explore datasets, transform variables, and summarize data using R. Tidystata supplies the main tools for routine recoding, tabulation, and numerical summaries; the tidyverse supplies general table operations and graphics.
The goal is not simply to teach R commands, but to help you develop the skills and habits needed for clear, efficient, and reproducible social-science research.
This edition includes twenty-eight written chapters, a preface, and a General Social Survey (GSS) data appendix. Technical recommendations and software comparisons are subject to further editorial review.
Who this book is for
This book is written for students and researchers in sociology, political science, economics, education, and related fields who want to learn R for data analysis.
No prior programming experience is required. Basic familiarity with variables, averages, and percentages is sufficient. We introduce R syntax from the beginning and explain statistical concepts as they become relevant.
If you have used Stata, SPSS, or another statistical package, you may find some concepts familiar. However, the book assumes no experience with statistical software.
Learning with real data
Social-science research involves more than writing code. Understanding what observations represent, how variables are measured, and how results should be interpreted is equally important.
Basic syntax examples use small numbers, strings, and logical values so that you can follow each operation directly. Data-handling and analysis lessons use real datasets, especially World Bank country indicators in lifeexp and the 2024 General Social Survey (GSS). Each chapter normally keeps one main dataset, and adjacent lessons reuse it where possible. Other datasets are introduced when they supply a needed feature, such as genuine dates.
For these data examples, the book explains the relevant sources, units of measurement, and transformations so that you can understand both the code and the underlying data.
How to use this book
The book is designed to be read progressively, with each chapter building on the skills introduced earlier.
Begin with Get started with R, then read 2 Objects in R for an overview of R objects and the reasons this book centers on tibbles. Enter Manipulate tables next. 3 Select columns introduces the packaged country data and the shared selection language. Sorting, the pipe, and observation selection then show how to work with one table and connect operations.
6 Select observations continues with the same country data. 8 Tabulate data introduces the 2024 GSS for tabulation, summaries, descriptive plots, and variable manipulation. Follow the one-time preparation recipe in Appendix A: GSS data at that point; later survey chapters reuse the saved extract.
Explore variables follows. 7 Inspect a dataset examines records and variable descriptions, then teaches a CSV download, import, and export. Continue to 8 Tabulate data, 9 Summarize data, and 11 Visualize data to describe categories, numerical distributions, and graphical patterns. Return to these inspection tools whenever a transformation needs checking.
Manipulate variables starts with 12 Create variables, then develops numeric and logical transformations in 14 Logicals and numbers, factors and questionnaire scores in 15 Strings and factors, and strings and dates in 16 Dates and times. Return to frequency tables and summaries after a transformation to check what changed.
Manage datasets covers 18 Import and export, 19 Group and reshape, and 20 Combine datasets: bring files into R, reorganize observations, and connect tables through checked identifiers. Analyze variables then uses 22 ggplot2 to build and refine figures, 23 Statistical tests to compare groups and test associations, 24 OLS regression for OLS models, and 25 Logit and probit for binary-outcome models.
The final part, Programming, is optional. It explains basic expressions, vectors, and other R data structures in more detail. Read it when you want to understand the machinery behind an operation; it is not a prerequisite for the practical chapters. Each practical chapter explains the syntax needed for its own tasks.
A few suggestions will help you get the most out of the book:
- Run the examples yourself. Follow the examples in order, beginning with the package and data setup provided in each chapter.
- Work with the data. Experiment with the code, inspect the results, and consider what the output means.
- Complete the exercises. Try solving problems independently before consulting the worked answers or an AI assistant.
- Keep your work organized. Save your scripts and data files so that you can return to previous exercises and reproduce your results.
Before working with the GSS examples, follow the instructions in Appendix A: GSS data to prepare and save the required data extract. Subsequent chapters will use this saved file.
Reading conventions
Throughout the book, a few conventions help distinguish essential material from supplementary information.
- Optional references provide additional explanations or resources. You can skip them on a first reading without missing the main lesson. The GSS applications state their data setup explicitly.
- Previews illustrate techniques that will be explained in greater detail later. Focus on understanding their purpose rather than memorizing every command.
- Installation instructions generally need to be run only once when setting up your environment. Other code blocks may demonstrate operations on your own files or optional software rather than code executed automatically by the book.
You do not need to understand every detail on your first reading. The most effective way to learn R is to practice regularly and return to earlier examples as your skills develop.
License
This book is freely available under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. Commercial use and distribution of adapted versions require permission.