M01-2 Introduction to Literate Programming-Application Assignment

Author

Juan De La Cruz

Published

January 27, 2026

Introduction:

Hello, this document contains all 10 completed exercises for M101-2 Introduction to Literate Programming Assignment for IBM 6530 Marketing Analytics class at Cal Poly Pomona for the Masters of Science: Digital Marketing & Analytics program. Each exercise demonstrates basic R coding, data manipulation, and visualization skills duirng Quarto.

1 Ex 1. Create a variable X

Create a variable called X and assign the text “This is my first assignment” to it.

X <- "This is my first assignment"
X
[1] "This is my first assignment"

2 Ex 2. Adding Texts in Base R

Add two pieces of text together using paste() and paste0() in Base R.

# Create two text variables
first <- "Hello"
second <- "world!"

# Combine using paste() — adds a space
paste(first, second)
[1] "Hello world!"
# Combine using paste0() — no space
paste0(first, second)
[1] "Helloworld!"

3 Ex 3. Create and modify a vector

Create a vector called Y with the numbers 2, 3, 4, and 5. Next, multiply the vector by 2 and save it as Y again.

# Create vector Y
Y <- c(2, 3, 4, 5)

# Multiply Y by 2 and overwrite Y
Y <- Y * 2

# Print Y to confirm
Y
[1]  4  6  8 10

4 Ex 4. Print variables X and Y

Print both the variable X and the vector Y.

# Print X
X
[1] "This is my first assignment"
# Print Y
Y
[1]  4  6  8 10

5 Ex 5. Maximum and minimum of Y

Show the maximum and minimum value of the vector Y that you created.

# Maximum value of Y
max(Y)
[1] 10
# Minimum value of Y
min(Y)
[1] 4

6 Ex 6. Plot unemployment duration over time

Using the economics dataset, create a line plot of uempmed over time using ggplot2.

# Load ggplot2
library(ggplot2)

# Create a line plot of unemployment duration over time
ggplot(data = economics, aes(x = date, y = uempmed)) +
  geom_line()

7 Ex 7. Add labels to the plot

Add a title and axis labels to the line plot of uempmed over time.

# Load ggplot2
library(ggplot2)

# Line plot with labels added
ggplot(data = economics, aes(x = date, y = uempmed)) +
  geom_line() +
  labs(
    title = "Median Unemployment Duration Over Time",
    x = "Year",
    y = "Median Unemployment Duration (weeks)"
  )

8 Ex 8. Filter the economics dataset

Filter the economics dataset to only include data from the year 2000, and print the first 6 rows.

# Load dplyr for filtering
library(dplyr)

# Filter for the year 2000 and show first 6 rows
economics %>%
  filter(format(date, "%Y") == "2000") %>%
  head()
# A tibble: 6 × 6
  date         pce    pop psavert uempmed unemploy
  <date>     <dbl>  <dbl>   <dbl>   <dbl>    <dbl>
1 2000-01-01 6535. 280976     5.4     5.8     5708
2 2000-02-01 6620. 281190     4.8     6.1     5858
3 2000-03-01 6686. 281409     4.5     6       5733
4 2000-04-01 6671. 281653     5       6.1     5481
5 2000-05-01 6708. 281877     4.9     5.8     5758
6 2000-06-01 6744. 282126     4.9     5.7     5651

9 Ex 9. Calculate mean unemployment duration for 2000

Using the filtered data from Exercise 8, calculate the mean of uempmed for the year 2000.

# Load dplyr
library(dplyr)

# Filter for the year 2000 and calculate mean unemployment duration
economics %>%
  filter(format(date, "%Y") == "2000") %>%
  summarise(mean_uempmed = mean(uempmed))
# A tibble: 1 × 1
  mean_uempmed
         <dbl>
1         5.93

10 Ex 10. Reflection

Write a short reflection (1–2 sentences) about what you learned in this assignment. Save it as a variable called reflection and print it.

# Create a reflection text
reflection <- "In this assignment, I learned how to use Quarto, write clean R code, and create visualizations with ggplot2. I also practiced filtering and summarizing data."

# Print the reflection
reflection
[1] "In this assignment, I learned how to use Quarto, write clean R code, and create visualizations with ggplot2. I also practiced filtering and summarizing data."

Conclusion:
This assignment was helpful for me to practice writing R codes, using Quarto for literate programming for the first time, summarizing data and creating visualizations with the different packages we used.