R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

install.packages("readxl")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
library(readxl)
my_data <- read_excel("advertising_randomized.xlsx")

## run the library
install.packages("tidyverse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
head(my_data)
## # A tibble: 6 × 6
##       X    X1     TV radio newspaper sales
##   <dbl> <dbl>  <dbl> <dbl>     <dbl> <dbl>
## 1    51    22 177.    6.13      1.91  13.6
## 2   161   110   7.84 14        46.8   10.8
## 3    17   126   4.11 23.3       6.42  13.0
## 4   183   147 114.   18.6      40.7   20.3
## 5   150   139 120.   11.6       6.35  20.5
## 6   139   121 210.   29.6       8.01  14.7
glimpse(my_data)
## Rows: 250
## Columns: 6
## $ X         <dbl> 51, 161, 17, 183, 150, 139, 170, 206, 258, 222, 54, 62, 13, …
## $ X1        <dbl> 22, 110, 126, 147, 139, 121, 21, 146, 107, 49, 21, 30, 230, …
## $ TV        <dbl> 176.99, 7.84, 4.11, 113.89, 120.06, 210.07, 237.35, 217.00, …
## $ radio     <dbl> 6.13, 14.00, 23.29, 18.60, 11.56, 29.62, 2.73, 53.80, 43.91,…
## $ newspaper <dbl> 1.91, 46.78, 6.42, 40.72, 6.35, 8.01, 8.49, 21.02, 34.64, 21…
## $ sales     <dbl> 13.60, 10.80, 13.04, 20.34, 20.51, 14.70, 17.39, 16.70, 15.0…
ggplot(data = my_data)

str(my_data)
## tibble [250 × 6] (S3: tbl_df/tbl/data.frame)
##  $ X        : num [1:250] 51 161 17 183 150 139 170 206 258 222 ...
##  $ X1       : num [1:250] 22 110 126 147 139 121 21 146 107 49 ...
##  $ TV       : num [1:250] 176.99 7.84 4.11 113.89 120.06 ...
##  $ radio    : num [1:250] 6.13 14 23.29 18.6 11.56 ...
##  $ newspaper: num [1:250] 1.91 46.78 6.42 40.72 6.35 ...
##  $ sales    : num [1:250] 13.6 10.8 13 20.3 20.5 ...
ggplot(
  data = my_data,
  mapping = aes(x = TV, y = sales)
)

str(my_data)
## tibble [250 × 6] (S3: tbl_df/tbl/data.frame)
##  $ X        : num [1:250] 51 161 17 183 150 139 170 206 258 222 ...
##  $ X1       : num [1:250] 22 110 126 147 139 121 21 146 107 49 ...
##  $ TV       : num [1:250] 176.99 7.84 4.11 113.89 120.06 ...
##  $ radio    : num [1:250] 6.13 14 23.29 18.6 11.56 ...
##  $ newspaper: num [1:250] 1.91 46.78 6.42 40.72 6.35 ...
##  $ sales    : num [1:250] 13.6 10.8 13 20.3 20.5 ...
ggplot(
  data = my_data,
  mapping = aes(x = TV, y = sales)
) +
  geom_point()

#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

ggplot(
  data = my_data,
  mapping = aes(x = TV, y = sales, color = cut(newspaper, breaks = 3))
) +
  geom_point()

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.