[1] "This is my first assignment"
M01-2-Introduction to Literate Programming-Application Assignment
1 Create a variable called X and assign the text “This is my first assignment” to it.
Use “<-” to assign a variable to the text.
Assigning the string to X:
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:
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "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."
[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"
[1] "1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th"
2.2 Using paste() function, add “and I’m loving it!” to X, the variable that you just created.
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 :
4 Print both the variable (X) and the vector (Y).
5 Show the maximum and the minimum value of the vector Y that you created.
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.
For this task, just type the three lines below inside a code chunk.
```{r}
install.packages(“ggplot2”)
If you have already installed the package before, you don’t need to run this code again.
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
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.
Once you replicate the chart, you can use #| inside the code chunk to control the label, caption, and size of the figure.
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")9 Using one of the callout blocks, describe your findings. In doing so, use cross-referencing to refer to the figure you created earlier.
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.