M01-2-Introduction to Literate Programming-Application Assignment

Author

Min Gong

Published

June 10, 2025

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

Hint

Use “<-” to assign a variable to the text.

Assigning the string to X:

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

2 Adding texts in Base R. Since there are two sub-exercises for this exercise, use sub-headings appropriately in Quarto.

2.1 Look up paste() function from help to learn how to use it. In the help document, you will find some use cases as examples toward the bottom of the page. Copy some of them and paste them into your codebook inside a code chunk right below this instruction. Execute one line at a time and learn how they work.

Look up the function and paste examples:

help("paste")
paste0(1:12)
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"
paste(1:12) 
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"
nth <- paste0(1:12, c("st", "nd", "rd", rep("th", 9)))
paste(month.abb, "is the", nth, "month of the year.")
 [1] "Jan is the 1st month of the year."  "Feb is the 2nd month of the year." 
 [3] "Mar is the 3rd month of the year."  "Apr is the 4th month of the year." 
 [5] "May is the 5th month of the year."  "Jun is the 6th month of the year." 
 [7] "Jul is the 7th month of the year."  "Aug is the 8th month of the year." 
 [9] "Sep is the 9th month of the year."  "Oct is the 10th month of the year."
[11] "Nov is the 11th month of the year." "Dec is the 12th month of the year."
paste(month.abb, letters)
 [1] "Jan a" "Feb b" "Mar c" "Apr d" "May e" "Jun f" "Jul g" "Aug h" "Sep i"
[10] "Oct j" "Nov k" "Dec l" "Jan m" "Feb n" "Mar o" "Apr p" "May q" "Jun r"
[19] "Jul s" "Aug t" "Sep u" "Oct v" "Nov w" "Dec x" "Jan y" "Feb z"
paste0(nth, collapse = ", ")
[1] "1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th"
Hint

This exercise is to illustrate how you can learn by using the sample codes available from most of the help documents for any functions in R. You may execute each line to see how paste() and paste0() work differently.

2.2 Using paste() function, add “and I’m loving it!” to X, the variable that you just created.

X <- paste(X, "and I'm loving it!", sep = ", ")
X
[1] "This is my first assignment, and I'm loving it!"

3 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.

Creating vector Y :

Y <- c(2,3,4,5)
Y
[1] 2 3 4 5
Y <- Y * 2
Y
[1]  4  6  8 10

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

min(Y)
[1] 4
max(Y)
[1] 10

6 Load up the ggplot2 package and take a look at the first six rows of the data called, “economics,” which is available with the package.

Hint

For this task, just type the three lines below inside a code chunk.

```{r}

install.packages(“ggplot2”)

Important

If you have already installed the package before, you don’t need to run this code again.


library(ggplot2)
head(economics)

#install.package(ggplot2)
library(ggplot2)
head(economics)
# A tibble: 6 × 6
  date         pce    pop psavert uempmed unemploy
  <date>     <dbl>  <dbl>   <dbl>   <dbl>    <dbl>
1 1967-07-01  507. 198712    12.6     4.5     2944
2 1967-08-01  510. 198911    12.6     4.7     2945
3 1967-09-01  516. 199113    11.9     4.6     2958
4 1967-10-01  512. 199311    12.9     4.9     3143
5 1967-11-01  517. 199498    12.8     4.7     3066
6 1967-12-01  525. 199657    11.8     4.8     3018

7 Use the data set “economics,” which is from ggplot2 package. First understand the data by referring to help document. Since there are only six variables, provide the definition of the variables nicely in bullets to aid readers who will not have access to the help document. Pick two variables that you think are somehow related. Then draw a chart using ggplot2 package. Assign “plot” to the basic layers of data, aesthetics, and geometry, which are the minimum elements to generate a chart according to the grammar of graphics. Further, using the function `labs()`, add the title and labels for the x-axis and y-axis.

Definition of Each Variables

visualization:

plot <- ggplot(economics, aes(x = pce, y = psavert))+
  geom_point(aes(alpha = 0.5), size = 3, show.legend = FALSE)

plot +
  labs(title = "Personal Consumption Expenditures vs. Personal Savings Rate",
       x = "Personal Consumption Expenditures (in billions of $)",
       y = "Personal Savings Rate")

Hint

All the variables are continuous, so you can use a scatter plot (geom_point) for the two variables. Next,  you might add this specification to the plot. The help document has unabridged labels for the variables. Use the full variable names for the x and y label in the chart.

```{r}

plot +

labs(title= “xxxxx xxxxxx xxxxxx”,

x = “xxxxx xxxxx”,

y = “xxx xxx”)

```

8 This time, replicate the chart above using a pipe operator and without saving the initial plot as a plot object. In other words, use one long chain of code from the data at the top of the lines to the last addition to the chart. Now, add a label and caption to the figure so that the figure to be created can be cross-referenced. Also, make the width of the figure 5 with an aspect ratio of 0.6.

Hint

Once you replicate the chart, you can use #| inside the code chunk to control the label, caption, and size of the figure.

Note

Figure 1 is a scatter plot for Personal Consumption Expenditures vs. Personal Savings Rate.

library(tidyverse)
economics %>% 
  ggplot(aes(x = pce, y = psavert))+
  geom_point(aes(alpha = 0.5), size = 3, show.legend = FALSE)+
  labs(title = "Personal Consumption Expenditures",
       subtitle = "vs. Personal Savings Rate",
       x = "Personal Consumption Expenditures (in billions of $)",
       y = "Personal Savings Rate")
Figure 1: Personal Consumption Expenditures vs. Personal Savings Rate

9 Using one of the callout blocks, describe your findings. In doing so, use cross-referencing to refer to the figure you created earlier. 

Note

From Figure 1, the data indicate that as personal consumption expenditures increase, the saving rate generally declines, which aligns with economic intuition. Interestingly, once expenditures reach the 10,000 billion mark, the saving rate begins to rise.

10 Make sure your codebook is well organized, easy to navigate, and beautiful. Implement as many tools as you learned in Step 1 to make your report reproducible and effective throughout the report. Then, explicitly state under this question what you did. You don’t need to mention multiple actions for the same function. Summarize the different functions you implemented using a numbered list. You must use at least 10 different kinds of tools listed below.

Summary of my implementations of different features :

  1. Section numbering with “\#”, “\##”, and “\###” to clearly structure the report
  2. Bold font to emphasize key tasks.
  3. Italicized font for strings and R codes in text.
  4. Callout-tip for all hints.
  5. Callout-note for my findings from the figure.
  6. Callout-important to emphasize some of the hint.
  7. Blockquote for every task description to keep consistency and clarity.
  8. Tick (“\”) before and after a function to set it apart from regular texts.
  9. Cross-reference the figure
  10. Optional argument (#|) inside some code chunks to control code chunks when necessary.
  11. Using different output format for this file, both html and PDF file.