Code
result <- 5 + 3Sebastian Koehler
January 28, 2026
Materials presented here build on the resources for instructors designed by Elena Llaudet and Kosuke Imai in Data Analysis for Social Science: A Friendly and Practical Introduction (Princeton University Press).
This week, we will cover the following topics:
R stores information in what are known as objects. Think of an object as a box that can contain anything. All we need to do is give it a name, so that we know how to refer to it, and specify its contents.
We use the assignment operator <- for creating or updating objects. If we wanted to save the result of adding 5 + 3, we would do the following:
Notice that after running the code above, the object will show up in the environment. To see what’s in result, just type and run the name of the object:
You can use objects to perform calculations. For example:
You can also store the result of this calculation in a new object. For example:
When naming objects in R, follow these simple rules:
_), or dots (.).@, #, or !).MyObject and myobject are treated as different objects.Here’s a table of examples showing correct and incorrect object names:
Table 1: Correct and Incorrect Object Names
| Correct Object Names | Incorrect Object Names |
|---|---|
my_data |
my data |
result |
1result |
data_2023 |
data-2023 |
x1 |
x@1 |
Functions in R are like predefined recipes that perform specific tasks. They take inputs (called arguments), process them, and return an output. For instance, rather than using the + symbol for addition, we can use the sum() function to combine two or more numbers:
Every function in R requires parentheses () to work. The arguments (inputs) are placed inside the parentheses. By default, the output is displayed on the screen, but you can also save it to an object for later use. For example:
Here, eight becomes an object that stores the output of the sum() function. Again, you can use this object in further calculations:
R provides built-in help files for every function, making it easy to understand how they work. Help files explain what a function does, its arguments (inputs), and what it returns (output). They also include examples to help you use the function.
Any function that you use in R will have an associated help file. To access a help file, you can use the help() function. For example, if you want to know how to use the sum() function, you could type help(sum) and look at the online documentation:
You could also use a question mark ? as a shortcut to access online help:
Alternatively, you can type the function name in the “Help” tab in the bottom-right pane of RStudio.
Here’s what you’ll find in a help file:
In R, the “working directory” is the folder where R looks for files and saves your work. You can think of it as the “home base” for your R session.
First, please follow these steps to organize your files efficiently:
POL272.POL272 folder, create a subfolder called data.POL272 folder as seminar1.R. Each week, start a new script and save it in this folder.Now, let’s set the working directory:
POL272 folder. This tells R to look for files and save outputs in that location. For example:If your POL272 folder is on your Desktop, use the following code:
If you are working on a Windows PC, you might use:
You can adjust the code above to tell R where to find the folder on your computer. For example, if your POL272 folder is inside a QMUL folder on your desktop, you could use setwd("~/Desktop/QMUL/POL272"). You can replace this path with the correct location of your folder, depending on where it is stored on your system.
You can also use RStudio’s built-in functionality to set the working directory:
Click on Session -> Set Working Directory -> Choose Directory.
Browse to your POL272 folder and click Open. RStudio will set the working directory for the current session, and you will see the corresponding setwd() command appear in the console.
You can copy the setwd() command from the console and paste it at the top of your script to ensure that the working directory is set consistently every time you run the script
Tip: Use getwd() to check your current working directory:
Create three separate objects named score1, score2, and score3 with the values 87.567, 92.345, and 78.912, respectively.
Use the sum() function to calculate the total of score1, score2, and score3, and save the result in an object called total_score.
Calculate the average of total_score and save it in an object called average_score.
Now open the help file for the round() function. In the help file, find out how to use the digits argument to control the number of decimal places when rounding a number.
Round average_score to one decimal place and save the result in an object called rounded_average.
Print total_score, average_score, and rounded_average to see the results.
#1: Create objects
score1 <- 87.567
score2 <- 92.345
score3 <- 78.912
#2: Calculate the total score
total_score <- sum(score1, score2, score3)
#3: Access the help file
help(round)
#4: Calculate the average score
average_score <- total_score / 3
#5: Round the average score
rounded_average <- round(average_score, digits = 1)
#6: Print the results
total_score[1] 258.824
[1] 86.27467
[1] 86.3