Lab 1: Introduction to R

Introduction

Name: ___________________________

Date: ___________________________

Introduction

The goal of this lab is to familiarize you with using R and RStudio. You’ll get hands-on practice with:

  • Performing simple calculations (basic arithmetic, mean, etc.).
  • Working with variables.
  • Creating simple variables.

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:

# Addition
5 + 3
[1] 8
# Subtraction
10 - 4
[1] 6
# Multiplication
6 * 7
[1] 42
# Division
20 / 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 age
my_age <- 25  # Replace 25 with your actual age

# Print the variable
my_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 scores
test_scores <- c(85, 92, 78, 96, 88, 91, 83)

# Display the vector
test_scores
[1] 85 92 78 96 88 91 83
# Calculate basic statistics
mean(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 = 15
random_data <- rnorm(50, mean = 100, sd = 15)

# Look at the first 10 values
head(random_data, 10)
 [1] 112.02204 110.12223 119.87493  97.65791  92.04070  70.09678 110.18699
 [8] 108.63955 100.14769  86.72960
# Calculate summary statistics for this data
mean(random_data)
[1] 102.2172
sd(random_data)
[1] 15.05372
min(random_data)
[1] 70.09678
max(random_data)
[1] 134.7615

Your turn: Create a vector of 30 random numbers with mean = 50 and sd = 5. Calculate its summary statistics.

# Write your code here:

Exercise 5: Basic Plotting

R has built-in functions for creating plots. Let’s make a histogram:

# Create a histogram of our random data
hist(random_data, 
     main = "Histogram of Random Data",
     xlab = "Value",
     ylab = "Frequency",
     col = "lightblue")

# Create a simple scatterplot
x_values <- c(1, 2, 3, 4, 5)
y_values <- c(2, 4, 1, 5, 3)

plot(x_values, y_values,
     main = "Simple Scatterplot",
     xlab = "X Values",
     ylab = "Y Values",
     pch = 16,  # Solid circles
     col = "red")

Your turn: Create a histogram of the family ages vector you created in Exercise 3. Add appropriate labels.

# Write your code here:

Submission Instructions

  1. Make sure all your code chunks run without errors
  2. Save this file with your name in the filename (e.g., “Lab1_YourLastName.qmd”)
  3. Render the document to HTML
  4. Submit both the .qmd file and the rendered HTML file to Canvas