Note that this lab will all be in “base R”, meaning we won’t (yet) be working with packages like tidyverse.
Exercise 1: Basic Arithmetic
R can be used as a calculator. Try the following operations:
# Addition5+3
[1] 8
# Subtraction10-4
[1] 6
# Multiplication6*7
[1] 42
# Division20/4
[1] 5
Your turn: Calculate \(15 + 8 * 3 - 2\)
# Write your code here:
Exercise 2: Variables and Assignment
In R, we can store values in variables using the assignment operator <-.
# Create a variable called 'my_age' and assign it your agemy_age <-25# Replace 25 with your actual age# Print the variablemy_age
[1] 25
Your turn: Create two variables representing the number of hours you studied yesterday and today. Calculate the total hours.
# Write your code here:
Exercise 3: Vectors and Basic Statistics
Vectors are collections of values. We create them using the c() function.
# Create a vector of test scorestest_scores <-c(85, 92, 78, 96, 88, 91, 83)# Display the vectortest_scores
[1] 85 92 78 96 88 91 83
# Calculate basic statisticsmean(test_scores) # Mean (average)
[1] 87.57143
median(test_scores) # Median (middle value)
[1] 88
sd(test_scores) # Standard deviation
[1] 6.078847
length(test_scores) # Number of values
[1] 7
Your turn: Create a vector with the ages of 5 family members or friends. Calculate the mean and standard deviation.
# Write your code here:
Exercise 4: Creating Data
Let’s create some sample data to work with:
# Create a vector of 50 random numbers from a normal distribution# with mean = 100 and standard deviation = 15random_data <-rnorm(50, mean =100, sd =15)# Look at the first 10 valueshead(random_data, 10)