# Numeric vector
<- c(85, 90, 76, 88, 92)
scores
# Character vector
<- c("Alice", "Bob", "Charlie", "David", "Eva")
students
# Logical vector
<- c(TRUE, TRUE, FALSE, TRUE, TRUE) passed
Tutorial 1: Introduction to R and Basic Data Types
Introduction to R
R is a powerful programming language widely used for statistical computing and data analysis. In this tutorial, we will cover the basics of R, including how to work with vectors and basic data types. By the end of this tutorial, you will be able to perform simple data operations in R and understand the fundamental data types.
1.1 Setting Up R and RStudio
To begin using R, youβll need to install R and RStudio. RStudio is an integrated development environment (IDE) that makes it easier to write R code.
- Installing R: Visit the CRAN website to download and install R.
- Installing RStudio: Download and install RStudio from the RStudio website.
Once installed, open RStudio, and youβre ready to start coding in R!
1.2 Basic Data Types in R
R has several basic data types that you will use frequently:
1.2.1 Vectors
Vectors are the most basic data structure in R. A vector is a sequence of data elements of the same basic type.
1.2.2 Data Types
R includes several fundamental data types:
- Numeric: Used for numbers. E.g.,
1
,3.14
,42
. - Character: Used for text strings. E.g.,
"apple"
,"R programming"
. - Logical: Used for TRUE or FALSE values. E.g.,
TRUE
,FALSE
. - Factor: Used for categorical data. E.g., levels like
"low"
,"medium"
,"high"
.
# Example of different data types
<- 25 # Numeric
age <- "Alice" # Character
name <- TRUE # Logical is_student
1.3 Basic Operations with Vectors
You can perform arithmetic operations on numeric vectors and use indices to subset them.
# Arithmetic operations
<- scores + 5 # Adding 5 to each score
total_score
# Subsetting vectors
<- students[which.max(scores)] # Finding the student with the highest score top_student
Exercises and Solutions
Exercise 1: Create and Manipulate Vectors
- Create a numeric vector called
ages
that contains the ages of five students:18
,21
,19
,22
,20
. - Subtract 2 from each element in the
ages
vector. - Find the maximum age in the
ages
vector.
Solution:
# Step 1: Create the ages vector
<- c(18, 21, 19, 22, 20)
ages
# Step 2: Subtract 2 from each element
<- ages - 2
adjusted_ages
# Step 3: Find the maximum age
<- max(adjusted_ages)
max_age max_age
[1] 20
Expected output:
[1] 20
Exercise 2: Working with Character Vectors
- Create a character vector called
subjects
that contains the names of three school subjects:"Math"
,"History"
,"Biology"
. - Add a new subject
"Physics"
to thesubjects
vector. - Extract the second subject from the
subjects
vector.
Solution:
# Step 1: Create the subjects vector
<- c("Math", "History", "Biology")
subjects
# Step 2: Add a new subject
<- c(subjects, "Physics")
subjects
# Step 3: Extract the second subject
<- subjects[2]
second_subject second_subject
[1] "History"
Expected output:
[1] "History"
Exercise 3: Logical Operations
- Create a logical vector called
attendance
with valuesTRUE
,FALSE
,TRUE
,TRUE
,FALSE
. - Count how many students attended (i.e., how many
TRUE
values there are). - Find out if all students attended by using the
all()
function.
Solution:
# Step 1: Create the attendance vector
<- c(TRUE, FALSE, TRUE, TRUE, FALSE)
attendance
# Step 2: Count how many students attended
<- sum(attendance)
count_attendance count_attendance
[1] 3
# Step 3: Check if all students attended
<- all(attendance)
all_attended all_attended
[1] FALSE
Exercise 4: Calculating Averages
- Using the
scores
vector from earlier, calculate the average score of the students. - Determine how many students scored above 80 using the
sum()
function.
Solution:
# Step 1: Calculate the average score
<- mean(scores)
average_score average_score
[1] 86.2
# Step 2: Count how many students scored above 80
<- sum(scores > 80)
students_above_80 students_above_80
[1] 4
Exercise 5: Creating and Using Factors
- Create a factor variable
grade_levels
with the levels"Freshman"
,"Sophomore"
,"Junior"
,"Senior"
. - Assign a grade level to each student in the
students
vector. - Display the frequency of each grade level using the
table()
function.
Solution:
# Step 1: Create the grade_levels factor
<- factor(c("Freshman", "Sophomore", "Junior", "Senior", "Freshman"),
grade_levels levels = c("Freshman", "Sophomore", "Junior", "Senior"))
# Step 2: Assign grade levels to students
<- data.frame(students, grade_levels)
students_grade_levels
# Step 3: Display the frequency of each grade level
<- table(students_grade_levels$grade_levels)
grade_frequency grade_frequency