Skip to main content

R for Computational Social Science

Author

Wouter van Atteveldt

Published

2026

Welcome

The goal of this book is to introduce data and text analysis for computational social scientists. It’s intended to be useful for people who have no prior experience with R or any other programming language.

This book is aimed at students who want to become (better) computational social scientists. This means that our exploration of technical issues (and R in general) is driven by substantive curiosity. We don’t want to learn new commands (just) because we are curious about how R works, but primarily because we want to use them to understand human behaviour and perhaps even improve the world. For this reason, we aim to have data sets and examples relevant to the social sciences, and will try to embed all technical examples in substantive social science questions.

A driving principle of the book is that the question should come before the answer. So, we will often ask you to do something that you don’t fully understand yet, and then provide the explanation later. The first real chapter of the book, Fun with R, takes this to its extreme: it’s intended as a ‘grand tour’ of all the cool and useful things you can do with R, and the later chapters give the explanations for all the commands used in that chapter. So, be curious, ask questions, but never worry if something is not immediately clear.

The book is very much not a programming manual. We will of course teach programming fundamentals such as operators or functions, but only where it is useful to know these concepts for a (computational) social scientist. This approach also sets the book apart from other resources, such as the fantastic R for Data Science book by Hadley Wickham and colleagues. My intention is for this book is to be geared towards (aspiring) social scientists and curiosity-driven learning through interactive application. The goal is also to extend beyond data processing and cover basic computational social science methods such as text analysis, LLMs, and multimodal analysis. Of course, which book works best for you depends on your learning style as well as your background, but I hope that at least for some students (and professors!) this will be a useful resource.

NoteStatus of this book

Note that this book is a work in progress. The first part is mostly done, with all chapters finished and currently being battle tested by my students (thanks, all!).

All finished chapters have examples, most have exercises per section, and all chapters from chapter 3 end in a small multiple-choice quiz to test understanding. So, even in its current state this book should be a good resource for teaching an introduction to data processing and visualization using tidyverse to social science students.

How to Use This Book

Each chapter mixes explanations with interactive code blocks — you can run and modify R code directly in your browser without installing anything. Look for cells marked with the [▷ Run Code] button like the one below:

We encourage every reader to run all the code blocks, and especially to play around. Do you understand what it does? What happens if you change something? How could the code be useful in a real world scenario?

It’s important to remember that you cannot break anything. Every time you call the code, it just starts from scratch. There is nothing bad that can happen, and you can always press ↻ Start Over to reset the example.

Most sections end with an exercise - look for blue frames marked ✏️ Exercise. These have a pre-defined solution and generally also one or more hints. If you run the code, your attempt will be checked against our solution. We would encourage you to try to solve the exercise without looking at the solution first, but even if you do look at the solution try to understand why it works.

There are also frames marked 🧩 Challenge. These will challenge you to either think about the outcomes a bit more deeply, or make some additional changes to the code. Generally there will be no single ‘right’ solution. Don’t feel stressed if you don’t immediately see the solution, very often the tools you need will be introduced in later sections.

Finally, there are frames marked with an 💡. These give more in-depth explanation about the code or concepts that were just introduced, and will explain many ‘gotchas’ that we’ve seen students struggle with before.

Every chapter (from chapter 3) ends with a multiple-choice quiz, often organized per section. These quizzes are intended to test your understanding of the main concepts, functions, and syntax introduced in the chapter.

AI Statement

Since we now require students to add AI statements to essays, I figured it was only fair to disclose my usage of AI as well. Claude was used extensively in writing this book, but paradoxically was not allowed to write any of it. All chapter text, but also the topics, structure, and bad puns are fully mine down to the last em-dash. Where AI helped was in three ways: (1) Before writing each part, I brainstormed with Claude over the best data that can be used as an example for the things I wanted to demonstrate, and we co-wrote the data gathering and preparation scripts in data/README.md. (2) After writing each part, I asked Claude to review it critically against the didactic principles laid out in the contributing guide, but without making any changes except for clear typos (of which there were many!). (3) Finally, it helped me scaffold the graded exercises and multiple choice questions and provide the code checks and feedback. These rules are codified in the instructions in CLAUDE.md. Claude also approved this statement 🤓