# Addition
2 + 3
[1] 5
# Subtraction
5 - 2
[1] 3
# Multiplication
4 * 6
[1] 24
# Division
8 / 2
[1] 4
Welcome to the R Programming Basics tutorial! This guide is designed to introduce you to the fundamental concepts and syntax of R programming. R is a powerful tool for data analysis, and by the end of this tutorial, you should be comfortable with performing basic operations and using R for data manipulation.
In this tutorial, we will cover: 1. Basic arithmetic operations 2. Creating variables 3. Working with data structures (vectors and data frames) 4. Basic functions in R 5. Loading and exploring data from files
You will have opportunities to practice these skills within each section.
R can perform basic arithmetic operations such as addition, subtraction, multiplication, and division.
Try out the following examples by running the code in the console or in the provided code chunk:
# Addition
2 + 3
[1] 5
# Subtraction
5 - 2
[1] 3
# Multiplication
4 * 6
[1] 24
# Division
8 / 2
[1] 4
In R, you can store values in variables for later use. To create a variable, you assign a value to a name using the <- operator. While = also works, <- is the standard assignment operator in R.
Comments in R code, which start with a #, are used to explain the code and are ignored when the code is run.
Try creating some variables below:
# Assigning the value 10 to a variable named 'x'
<- 10
x
# Assigning the value 5 to a variable named 'y'
<- 5
y
# Performing an operation and storing the result in 'z'
<- x + y
z
# Printing the value of 'z'
z
[1] 15
In this case, x
stores the value 10
, y
stores 5
, and z
stores the sum of x
and y
.
R provides various data structures. Let’s get familiar with vectors and data frames.
A vector is a basic data structure that holds a sequence of values of the same type. You can create a vector using the c()
function (which stands for “combine”).
# Create a vector of numbers
<- c(1, 2, 3, 4, 5)
numbers
numbers
[1] 1 2 3 4 5
A data frame is similar to a spreadsheet or table, where each column can contain data of a different type. You can create a data frame using the data.frame()
function..
# Create a data frame for student data
<- data.frame(
student_data student_id = c(101, 102, 103),
quiz_score = c(85, 92, 88),
time_on_task = c(25, 30, 20)
)
# View the data frame
student_data
student_id quiz_score time_on_task
1 101 85 25
2 102 92 30
3 103 88 20
This data frame contains three columns: student_id
, quiz_score
, and time_on_task
.
Functions in R perform specific tasks, such as performing calculations, summarizing data, and manipulating variables. Let’s look at a few common functions.
# Sum of numbers in a vector
sum(numbers)
[1] 15
# Calculate the mean of numbers in a vector
mean(numbers)
[1] 3
You can access a specific column from a data frame using the $
operator.
# Accessing the 'quiz_score' column from the data frame
$quiz_score student_data
[1] 85 92 88
You can load data into R from various formats (CSV, Excel, etc.) and manipulate it. Let’s walk through loading a dataset from a CSV file.
Before running the code below, upload a CSV file to your Posit Cloud space using the “Files” tab. For this example, let’s assume your file is named ’student_quiz_scores.csv`
To load a dataset from a CSV file, you can use the read.csv()
function:
# Load a CSV file
<- read.csv("data/student_quiz_scores.csv")
grades_data
# Use `head()` to view the first few rows of the data
head(grades_data)
Student_ID Quiz Score
1 Student_1 Quiz_1 88
2 Student_2 Quiz_1 78
3 Student_3 Quiz_1 64
4 Student_4 Quiz_1 92
5 Student_5 Quiz_1 57
6 Student_6 Quiz_1 70
# Use `str()` to view the structure of the data frame
str(grades_data)
'data.frame': 400 obs. of 3 variables:
$ Student_ID: chr "Student_1" "Student_2" "Student_3" "Student_4" ...
$ Quiz : chr "Quiz_1" "Quiz_1" "Quiz_1" "Quiz_1" ...
$ Score : int 88 78 64 92 57 70 88 68 72 60 ...
The head()
function shows you the first few rows, and str()
shows you the variable types and structure, so you can quickly check if the data loaded correctly.
Now that you have your data loaded, you can filter it using bassic R Syntax.
# Filter the data to include only rows where 'Score' is greater than 85
<- grades_data[grades_data$Score > 85, ]
filtered_data
# View the filtered data
filtered_data
Student_ID Quiz Score
1 Student_1 Quiz_1 88
4 Student_4 Quiz_1 92
7 Student_7 Quiz_1 88
14 Student_14 Quiz_1 89
20 Student_20 Quiz_1 93
22 Student_22 Quiz_1 87
28 Student_28 Quiz_1 93
30 Student_30 Quiz_1 98
32 Student_32 Quiz_1 91
36 Student_36 Quiz_1 96
37 Student_37 Quiz_1 93
39 Student_39 Quiz_1 86
43 Student_43 Quiz_1 88
48 Student_48 Quiz_1 99
54 Student_4 Quiz_2 96
56 Student_6 Quiz_2 93
58 Student_8 Quiz_2 96
63 Student_13 Quiz_2 99
64 Student_14 Quiz_2 89
68 Student_18 Quiz_2 91
73 Student_23 Quiz_2 93
79 Student_29 Quiz_2 97
84 Student_34 Quiz_2 89
87 Student_37 Quiz_2 94
89 Student_39 Quiz_2 96
93 Student_43 Quiz_2 94
94 Student_44 Quiz_2 90
97 Student_47 Quiz_2 94
104 Student_4 Quiz_3 93
114 Student_14 Quiz_3 91
115 Student_15 Quiz_3 88
116 Student_16 Quiz_3 90
124 Student_24 Quiz_3 97
127 Student_27 Quiz_3 86
129 Student_29 Quiz_3 93
130 Student_30 Quiz_3 89
136 Student_36 Quiz_3 86
137 Student_37 Quiz_3 96
144 Student_44 Quiz_3 88
150 Student_50 Quiz_3 91
153 Student_3 Quiz_4 88
154 Student_4 Quiz_4 98
158 Student_8 Quiz_4 86
160 Student_10 Quiz_4 88
161 Student_11 Quiz_4 94
163 Student_13 Quiz_4 92
172 Student_22 Quiz_4 91
173 Student_23 Quiz_4 94
177 Student_27 Quiz_4 93
178 Student_28 Quiz_4 93
184 Student_34 Quiz_4 88
189 Student_39 Quiz_4 90
191 Student_41 Quiz_4 88
196 Student_46 Quiz_4 86
198 Student_48 Quiz_4 91
199 Student_49 Quiz_4 93
205 Student_5 Quiz_5 90
206 Student_6 Quiz_5 98
207 Student_7 Quiz_5 98
209 Student_9 Quiz_5 88
212 Student_12 Quiz_5 98
213 Student_13 Quiz_5 86
214 Student_14 Quiz_5 98
216 Student_16 Quiz_5 98
221 Student_21 Quiz_5 86
227 Student_27 Quiz_5 93
235 Student_35 Quiz_5 87
242 Student_42 Quiz_5 97
259 Student_9 Quiz_6 90
261 Student_11 Quiz_6 89
262 Student_12 Quiz_6 88
264 Student_14 Quiz_6 89
269 Student_19 Quiz_6 99
273 Student_23 Quiz_6 91
278 Student_28 Quiz_6 97
279 Student_29 Quiz_6 98
282 Student_32 Quiz_6 97
285 Student_35 Quiz_6 86
288 Student_38 Quiz_6 90
291 Student_41 Quiz_6 97
299 Student_49 Quiz_6 98
302 Student_2 Quiz_7 87
303 Student_3 Quiz_7 89
306 Student_6 Quiz_7 97
312 Student_12 Quiz_7 95
317 Student_17 Quiz_7 90
324 Student_24 Quiz_7 97
331 Student_31 Quiz_7 97
335 Student_35 Quiz_7 95
337 Student_37 Quiz_7 87
338 Student_38 Quiz_7 87
339 Student_39 Quiz_7 94
348 Student_48 Quiz_7 96
351 Student_1 Quiz_8 97
355 Student_5 Quiz_8 98
357 Student_7 Quiz_8 93
361 Student_11 Quiz_8 95
364 Student_14 Quiz_8 90
365 Student_15 Quiz_8 86
368 Student_18 Quiz_8 98
369 Student_19 Quiz_8 95
380 Student_30 Quiz_8 91
383 Student_33 Quiz_8 89
384 Student_34 Quiz_8 95
386 Student_36 Quiz_8 99
388 Student_38 Quiz_8 96
393 Student_43 Quiz_8 91
397 Student_47 Quiz_8 89
400 Student_50 Quiz_8 92
Perform some basic arithmetic operations using numbers of your choice.
Create variables for two numbers and add them together.
# Perform some basic arithmetic operations using numbers of your choice.
4 + 5
[1] 9
10 -5
[1] 5
8*4
[1] 32
27/9
[1] 3
# Create variables for two numbers and add them together.
=4
number1=8
number2= number1 + number2
sum_result+ number2 number1
[1] 12
# Create a vector of your favorite numbers and calculate the sum and mean of the numbers.
<- c(8,13,14)
numbers sum(numbers)
[1] 35
mean(numbers)
[1] 11.66667
# Modify your vector by adding a new number.
<- c(3,8,13,14)
numbers sum(numbers)
[1] 38
mean(numbers)
[1] 9.5
# Create a small data frame with your name, age, and favorite color.
<-c("Abby")
name <- c(22)
age <- c("blue")
color <- data.frame(Name = "Abby", Age = 22, Color = "blue")
my_dataframe print(my_dataframe)
Name Age Color
1 Abby 22 blue
# Access and print the age column of your data frame.
print(my_dataframe[["Age"]])
[1] 22
Use the sum()
and mean()
functions to analyze the vector you created earlier.
# Use the `sum()` and `mean()` functions to analyze the vector you created earlier.
<- c(8,13,14)
my_vector <- sum(my_vector)
total_sum sum(my_vector)
[1] 35
<- mean(my_vector)
average_mean mean(my_vector)
[1] 11.66667
Upload a CSV file to Posit Cloud and load it into R.
2. Use the head()
function to view the first few rows.
3. Perform a basic filter operation on your dataset (e.g., filtering based on a specific column).
# Upload a CSV file to Posit Cloud and load it into R.
read.csv("data/pacer_scores.csv")
Student.. Pacer.Score
1 1 18
2 2 24
3 3 27
4 4 18
5 5 32
6 6 35
7 7 42
8 8 23
9 9 36
10 10 24
11 11 23
12 12 30
13 13 31
14 14 38
15 15 29
<- read.csv("data/pacer_scores.csv")
pacer_scores
# Use the head() function to view the first few rows.
head(pacer_scores)
Student.. Pacer.Score
1 1 18
2 2 24
3 3 27
4 4 18
5 5 32
6 6 35
# Perform a basic filter operation on your dataset.
<- pacer_scores[pacer_scores$`Pacer Score` >27, ]
filtered_data filtered_data
[1] Student.. Pacer.Score
<0 rows> (or 0-length row.names)
Great job! You’ve just taken your first steps in learning R. These basics will be the foundation for more advanced data analysis techniques as you continue to work with R. Make sure to practice regularly and feel free to experiment with the concepts you’ve learned here.
To receive full credit, you will need to render this document and publish it via a method such as: Quarto Pub, Posit Cloud, RPubs , GitHub Pages, or other methods. Once you have shared a link to you published document with me and I have reviewed your work, you will be officially done with the current module.
Complete the following steps to submit your work for review by:
First, change the name of the author:
in the YAML header at the very top of this document to your name. The YAML header controls the style and feel for knitted document but doesn’t actually display in the final output.
Next, click the “Render” button in the toolbar above to “render” your R Markdown document to a HTML file that will be saved in your R Project folder. You should see a formatted webpage appear in your Viewer tab in the lower right pan or in a new browser window. Let me know if you run into any issues with rendering.
Finally, publish. To do publish, follow the step from https://docs.posit.co/cloud/guide/publish/#publish-from-a-cloud-project
If you have any questions about this module, or run into any technical issues, don’t hesitate to contact me.
Once I have checked your link, you will be notified!