greeting <- "hello world" # assigns the value 'hello world' to variable [greeting]
print(greeting) # print the value of [greeting] to the console[1] "hello world"
The pre-class reading for this section covered a number of topics, which we’ll revise today:
Variables and Assignment
Data Types in R
Vectors and Vector Operations
We can create variables, operate on variables, and update variables.
Create:
greeting <- "hello world" # assigns the value 'hello world' to variable [greeting]
print(greeting) # print the value of [greeting] to the console[1] "hello world"
Operate:
x <- 10
y <- 5
sum <- x + y # adds the values of each variable
print(sum)[1] 15
Update:
turn <- 0 # set [turn] to zero
turn <- turn + 1 # increment [turn] by 1
print(turn) # print updated value of 'turn'[1] 1
Objective: Learn how to assign a value to a variable.
print() function.x <- 10
y <- 25
print(x) # Should output: [1] 10
print(y) # Should output: [1] 25Objective: Understand how to use variables in arithmetic operations.
a <- 5
b <- 3
sum_result <- a + b
product_result <- a * b
print(sum_result) # Should output: [1] 8
print(product_result) # Should output: [1] 15Objective: Learn how to update the value of a variable.
Assign the value 50 to a variable named [total].
Increase the value of [total] by 20 using the <- operator.
Decrease the value of [total] by 10.
Print the updated value of [total].
total <- 50
total <- total + 20
total <- total - 10
print(total) # Should output: [1] 60Objective: Practice using variables in a more complex expression.
[area] = [length] * [width].[perimeter] = 2 * ([length] + [width]).length <- 4
width <- 6
area <- length * width
perimeter <- 2 * (length + width)
print(area) # Should output: [1] 24
print(perimeter) # Should output: [1] 20Objective: Understand how to manipulate and swap variable values.
m <- 7
n <- 14
# Swapping values
temp <- m
m <- n
n <- temp
print(m) # Should output: [1] 14
print(n) # Should output: [1] 7Objective: Learn to identify and understand different data types.
class() function.num <- 42
class(num) # Should output: [1] "numeric"
name <- "John Doe"
class(name) # Should output: [1] "character"
is_raining <- TRUE
class(is_raining) # Should output: [1] "logical"Objective: Practice converting data between different types.
a <- 5
a_char <- as.character(a)
a_num <- as.numeric(a_char)
print(a_char) # Should output: [1] "5"
print(class(a_char)) # Should output: [1] "character"
print(a_num) # Should output: [1] 5
print(class(a_num)) # Should output: [1] "numeric"Objective: Understand how logical values work in R.
x <- 10
y <- 15
is_greater <- x > y
is_equal <- x == y
print(is_greater) # Should output: [1] FALSE
print(is_equal) # Should output: [1] FALSEObjective: Get familiar with the factor data type, often used for categorical data.
class().levels() function to display the levels of [colour_factor].colours <- c("red", "blue", "green", "blue", "red")
colour_factor <- factor(colours)
print(colour_factor)
# Should output:
# [1] red blue green blue red
# Levels: blue green red
print(class(colour_factor)) # Should output: [1] "factor"
print(levels(colour_factor)) # Should output: [1] "blue" "green" "red"Objective: Explore how combining different data types in a vector results in coercion to a single data type.
class().mixed <- c(5, "hello", TRUE)
print(mixed)
# Should output: [1] "5" "hello" "TRUE"
print(class(mixed)) # Should output: [1] "character"
# Try a numeric operation
# mixed_numeric <- mixed + 2 ### Note this line should be run, but has to be commented as it returns an error
# This will result in an error because the vector is of character type.Objective: Understand how to check the length of vectors and why it’s important.
length() function to check the length of both vectors.numbers <- c(10, 20, 30, 40, 50)
letters <- c("a", "b", "c")
print(length(numbers)) # Should output: [1] 5
print(length(letters)) # Should output: [1] 3These tasks will help you implement some of the techniques discussed earlier in the tutorial.
Try to complete these tasks without looking at the solutions!
Write an R script to create a vector named [player_id] that contains the integers from 1 to 10.
player_id <- 1:10
print(player_id)Create two vectors [var_a] and [var_b]. [var_a] should contain numbers from 1 to 5, and [var_b] contain numbers from 6 to 10. Then add, subtract, multiply, and divide these vectors.
a <- 1:5
b <- 6:10
print(a + b)
print(a - b)
print(a * b)
print(a / b)Create a vector [total_goals] with the values 15, 23, 17, 21, 19. Use a logical operator to determine which values stored in the vector [total_goals] are greater than 18.
total_goals <- c(15, 23, 17, 21, 19)
print(total_goals > 18)With the vector [total_goals] from the previous exercise, use indexing to print the 2nd and 4th values in the vector [total_goals].
print(total_goals[c(2,4)])Working with the vector [total_goals], find the minimum, maximum, sum, and mean of the [total_goals].
Print these values to the console window.
print(min(total_goals))
print(max(total_goals))
print(sum(total_goals))
print(mean(total_goals))Create a named vector [players_signed] that contains the number of forwards, midfielders, defense, and goalkeepers. Set the values to 10, 8, 8, and 3, respectively.
players_signed <- c(forwards = 10, midfielders = 8, defense = 8, goalkeepers = 3)
print(players_signed)Using the vector [players_signed] from the previous exercise, add 5 to the number of forwards, subtract 2 from the number of midfielders, and print the updated vector.
players_signed["forwards"] <- players_signed["forwards"] + 5
players_signed["midfielders"] <- players_signed["midfielders"] - 2
print(players_signed)