Seminar 1: Introduction to R

Author

Sebastian Koehler

Published

January 28, 2026

Introduction to R

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).

Seminar Objectives

This week, we will cover the following topics:

  • Objects
  • Functions
  • Help files
  • Setting working directories

Objects

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:

Code
result <- 5 + 3

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:

Code
result 
[1] 8

You can use objects to perform calculations. For example:

Code
result*2 
[1] 16

You can also store the result of this calculation in a new object. For example:

Code
new_result <- result*2 

When naming objects in R, follow these simple rules:

  • Object names must start with a letter (A-Z or a-z).
  • They can contain letters, numbers (0-9), underscores (_), or dots (.).
  • Object names cannot have spaces or special characters (like @, #, or !).
  • R is case-sensitive, so 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

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:

Code
sum(5, 3)
[1] 8

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:

Code
eight <- sum(5, 3)

Here, eight becomes an object that stores the output of the sum() function. Again, you can use this object in further calculations:

Code
eight*2
[1] 16

Help files

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:

Code
help(sum)

You could also use a question mark ? as a shortcut to access online help:

Code
?sum

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:

  • Description: A brief summary of the function.
  • Usage: How to write the function, including all its arguments.
  • Arguments: Explanation of what each argument does.
  • Details: Additional information about the function.
  • Value: What the function returns as output.
  • Examples: Sample code showing how to use the function.

Setting working directories

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:

  • Create a folder on your computer named POL272.
  • Inside the POL272 folder, create a subfolder called data.
  • Save the script you are working on now in the POL272 folder as seminar1.R. Each week, start a new script and save it in this folder.

Now, let’s set the working directory:

  • At the beginning of your script, set the working directory to point to the 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:

Code
setwd("~/Desktop/POL272")

If you are working on a Windows PC, you might use:

Code
setwd("C:/Desktop/POL272")

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:

Code
getwd()

Exercises

  1. Create three separate objects named score1, score2, and score3 with the values 87.567, 92.345, and 78.912, respectively.

  2. Use the sum() function to calculate the total of score1, score2, and score3, and save the result in an object called total_score.

  3. Calculate the average of total_score and save it in an object called average_score.

  4. 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.

  5. Round average_score to one decimal place and save the result in an object called rounded_average.

  6. Print total_score, average_score, and rounded_average to see the results.

Show Solution
Code
#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
Code
average_score
[1] 86.27467
Code
rounded_average
[1] 86.3