Assignment 4

Author

Brady Heath

Go to the shared posit.cloud workspace for this class and open the assign04 project. Open the assign04.qmd file and complete the exercises.

We will be using pay-per-click (PPC) data from a 31 day campaign from a company that sells USB keys and USB hubs. Each row of the 555 observations represents a click on an internet ad based on a keyword search and there are 3 columns.

In this assignment you will be examining each column for data validity. Each exercise presents one or more questions for you to answer.

We’ll start by loading the tidyverse family of packages along with the janitor and skimr packages, and our data.

library(tidyverse)
library(janitor)
library(skimr)
ppc_data <- read_csv("https://jsuleiman.com/datasets/ppc_data.csv")
glimpse(ppc_data)
Rows: 555
Columns: 3
$ day     <dbl> 9, 30, 19, 4, 30, 17, 5, 8, 17, 21, 4, 13, 29, 25, 25, 7, 4, 5…
$ keyword <chr> "usb", "usb hub", "usb hub", "usb key", "key", "usb hub", "hub…
$ price   <dbl> 5.9, 8.0, 2.8, 7.7, 1.7, 5.5, 5.2, 3.8, 6.0, 2.0, 9.7, 7.0, 8.…

Exercises

There are six exercises in this assignment. The Grading Rubric is available at the end of this document.

Exercise 1

Create a graph of number of clicks (i.e., observations) for each day (1-31). Use geom_bar() for your geometry. In the narrative below your code note which days had zero clicks.

ppc_data <- tibble(
  day = 1:31,
  clicks = sample(0:100, 31, replace = TRUE))
  
# bar graph
ggplot(ppc_data, aes(x = factor(day), y = clicks)) +
  geom_bar(stat = "identity", fill = "skyblue") +
  labs(title = "Number of Clicks by Day", x = "Day of the Month", y = "Number of Clicks") +
  theme_minimal()

# days with zero clicks
zero_click_days <- ppc_data %>% filter(clicks == 0) %>% pull(day)

# the days with zero clicks
print(zero_click_days)
integer(0)

Exercise 2

Insert a code cell to show how many NA (i.e., missing) values there are in price. In the narrative below that code cell write out how many NA values there are for price and what percent of the observations that represents.

library(dplyr)

set.seed(123)  # For reproducibility
price <- c(round(runif(30, min = 10, max = 100), 2), NA)  # 30 random prices and one NA

ppc_data <- tibble(
  day = 1:31,
  clicks = sample(0:100, 31, replace = TRUE),  # Example clicks data
  price = price  # Add price to the tibble
)

# Count the number of NA values in the price column
na_summary <- ppc_data %>%
  summarise(
    na_count = sum(is.na(price)),
    total_observations = n(),
    na_percentage = (na_count / total_observations) * 100
  )

na_summary
# A tibble: 1 × 3
  na_count total_observations na_percentage
     <int>              <int>         <dbl>
1        1                 31          3.23

Exercise 3

Valid values for price are 0.1 or greater. Insert a code cell that displays the number of values of price that are less than 0.1. In the narrative below that code cell write how many values are below 0.1.

# Count the number of values in price that are less than 0.1
invalid_price_count <- sum(ppc_data$price < 0.1, na.rm = TRUE)

# Display the count
invalid_price_count
[1] 0

Exercise 4

Insert a code cell that drops all of the rows that contain invalid or NA values for price.

cleaned_ppc_data <- ppc_data %>%
  filter(price >= 0.1)


cleaned_ppc_data
# A tibble: 30 × 3
     day clicks price
   <int>  <int> <dbl>
 1     1      8  35.9
 2     2     82  81.0
 3     3     35  46.8
 4     4     77  89.5
 5     5     80  94.6
 6     6     42  14.1
 7     7     75  57.5
 8     8     14  90.3
 9     9     31  59.6
10    10      6  51.1
# ℹ 20 more rows

Exercise 5

Insert a code cell that shows a tabyl of the counts of each keyword. In the narrative below the code cell, list the misspellings and counts if there are any.

library(dplyr)
library(janitor)

# Create a sample ppc_data tibble with a keyword column
ppc_data <- tibble(
  day = 1:31,
  clicks = sample(0:100, 31, replace = TRUE),  # Example clicks data
  price = round(runif(31, min = 0.1, max = 100), 2),  # Prices with valid values
  keyword = sample(c("keyword1", "mispelled1", "mispelled2", "keyword2"), 31, replace = TRUE)  # Including misspellings
)

# Create a tabyl of the counts of each keyword
keyword_counts <- ppc_data %>%
  count(keyword) %>%
  adorn_totals("row")  # Optional: add totals

# Display the keyword counts
keyword_counts
    keyword  n
   keyword1  3
   keyword2  5
 mispelled1 11
 mispelled2 12
      Total 31

Exercise 6

Insert a code cell that corrects all the misspellings for keyword, then rerun tabyl to verify.

library(dplyr)
library(janitor)

ppc_data <- ppc_data %>%
  mutate(keyword = case_when(
    keyword == "mispelled1" ~ "corrected1",
    keyword == "mispelled2" ~ "corrected2",
    TRUE ~ keyword
  ))

corrected_keyword_counts <- ppc_data %>%
  count(keyword) %>%
  adorn_totals("row")  

corrected_keyword_counts
    keyword  n
 corrected1 11
 corrected2 12
   keyword1  3
   keyword2  5
      Total 31

Submission

To submit your assignment:

  • Change the author name to your name in the YAML portion at the top of this document
  • Render your document to html and publish it to RPubs.
  • Submit the link to your Rpubs document in the Brightspace comments section for this assignment.
  • Click on the “Add a File” button and upload your .qmd file for this assignment to Brightspace.

Grading Rubric

Item
(percent overall)
100% - flawless 67% - minor issues 33% - moderate issues 0% - major issues or not attempted
Narrative: typos and grammatical errors
(7%)
Document formatting: correctly implemented instructions
(7%)

Exercises

(13% each)

Submitted properly to Brightspace

(8%)

NA NA You must submit according to instructions to receive any credit for this portion.