R Programming Basics Tutorial

Author

Abby Shoulders

Introduction

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.

1. Basic Arithmetic Operations

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

2. Creating Variables

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'
x <- 10

# Assigning the value 5 to a variable named 'y'
y <- 5

# Performing an operation and storing the result in 'z'
z <- x + y

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

3. Working with Data Structures

R provides various data structures. Let’s get familiar with vectors and data frames.

Vectors

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
numbers <- c(1, 2, 3, 4, 5)

numbers
[1] 1 2 3 4 5

Data Frames

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
student_data <- data.frame(
  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.

4. Basic Functions in R

Functions in R perform specific tasks, such as performing calculations, summarizing data, and manipulating variables. Let’s look at a few common functions.

Summing Numbers

# Sum of numbers in a vector 
sum(numbers)
[1] 15

Mean of Numbers

# Calculate the mean of numbers in a vector 
mean(numbers)
[1] 3

Subsetting Data Frames

You can access a specific column from a data frame using the $ operator.

# Accessing the 'quiz_score' column from the data frame
student_data$quiz_score
[1] 85 92 88

5. Loading and Manipulating Data from a File

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.

Step 1: Uploading the Dataset to Posit Cloud

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`

Step 2: Loading the Dataset

To load a dataset from a CSV file, you can use the read.csv() function:

# Load a CSV file
grades_data <- read.csv("data/student_quiz_scores.csv")

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

Step 3: Basic Data Manipulation

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
filtered_data <- grades_data[grades_data$Score > 85, ]

# 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

Practice Activities

Activity 1: Arithmetic and Variables

  1. Perform some basic arithmetic operations using numbers of your choice.

  2. Create variables for two numbers and add them together.

# Perform some basic arithmetic operations using numbers of your choice.

# Create variables for two numbers and add them together.

Activity 2: Working with Vectors

  1. Create a vector of your favorite numbers and calculate the sum and mean of the numbers.
  2. Modify your vector by adding a new number.
# Create a vector of your favorite numbers and calculate the sum and mean of the numbers.


# Modify your vector by adding a new number.

Activity 3: Data Frame Practice

  1. Create a small data frame with your name, age, and favorite color.
  2. Access and print the age column of your data frame.
# Create a small data frame with your name, age, and favorite color.


# Access and print the age column of your data frame.

Activity 4: Using Functions

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.

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.



# he



# Perform a basic filter operation on your dataset.

Conclusion

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.

Render & Submit

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:

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

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

  3. 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!