Module 12. Assignment


This task was more challenging than I anticipated. I needed to do further study and troubleshooting before comprehending the functionality of R Markdown. I acquired foundational knowledge of Markdown formatting and the composition of basic LaTeX mathematics; nonetheless, the most arduous aspect was ensuring that each code segment was properly started and terminated. A single missed backtick resulted in problems while knitting.

I also learnt how narrative text and code segments collaborate to produce a coherent, repeatable report. Despite requiring time and patience, the completion of this exercise significantly enhanced my comprehension of the R Markdown structure.





R-CODE 

---

title: "My R Markdown Primer"

author: "Shanzay Khan"

date: "`r Sys.Date()`"

output: html_document

---


## Introduction


R Markdown is a document format that lets you combine **narrative text**, **R code**, and **math formulas** in a single file with the `.Rmd` extension. Instead of running code in R and then copying the results into Word, you keep everything together. When you *knit* the document, R runs all the code chunks and converts the file into a clean HTML (or PDF/Word) report with your text, tables, and plots.


This makes your work more **reproducible**, because anyone with your `.Rmd` file can re-run the analysis and recreate the same output.


---


## Narrative Text and LaTeX Math


R Markdown uses simple **Markdown** syntax for formatting text. For example:


- `#` starts a big heading  

- `##` starts a smaller heading  

- `**bold**` → **bold**  

- `*italic*` → *italic*  

- `-` creates bullet points  


R Markdown also supports **LaTeX math**. An inline math expression like  

`$\\alpha + \\beta = \\gamma$` will render as: $\alpha + \beta = \gamma$.


A displayed equation (on its own line) uses double dollar signs:


$$

\bar{x} = \frac{1}{n} \sum_{i = 1}^{n} x_i

$$


This is the formula for the sample mean: you add up all values $x_i$ and divide by the number of observations $n$.


---


## R Code Chunks: Loading a Library and Dataset


R code goes inside **code chunks**, which start with three backticks and `{r}` and end with three backticks. When you knit, R runs all of these chunks in order.


The chunk below loads a library and explores a built-in dataset:


```{r setup, message = FALSE}

# Load ggplot2 for plotting

library(ggplot2)


# Use the built-in mtcars dataset

head(mtcars)

```

summary(mtcars$mpg)

 R-output

> library(ggplot2)
> head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
> summary(mtcars$mpg)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  10.40   15.43   19.20   20.09   22.80   33.90 
> summary(mtcars$mpg)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  10.40   15.43   19.20   20.09   22.80   33.90 
> ggplot(mtcars, aes(x = hp, y = mpg)) +
+ geom_point() +
+ labs(
+ title = "Horsepower vs. Miles per Gallon (mtcars)",
+ x = "Horsepower (hp)",
+ y = "Miles per Gallon (mpg)"


https://github.com/shanzay28/r-programming-assignments/tree/main/Module-12

 

Comments

Popular posts from this blog

Module 6 – Linear Algebra in R (Part 2)

Assignment

Assignment #5