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.
day - represents the day of the campaign. Valid days are 1-31.
price - represents the price of the campaign. Price can’t be a number below 0.10
keyword - represents the keyword purchased. Everything must be spelled correctly, there aren’t many keywords but they are some combination of “usb” and/or “key” or “hub”
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.
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.
# Load necessary librarieslibrary(ggplot2)library(dplyr)# Sample dataset: clicks per day (1-31)clicks_data <-data.frame(day =1-31,clicks =c(5, 0, 8, 2, 0, 0, 4, 10, 0, 3, 1, 0, 0, 7, 6, 0, 0, 0, 2, 0, 5, 0, 3, 8, 0, 0, 4, 1, 0, 0, 9, 0, 6))# Create a long format dataset for geom_bar()long_data <- clicks_data %>%uncount(clicks, .id ="click_id") %>%mutate(click_id =row_number())# Create the bar graph using geom_bar()ggplot(long_data, aes(x =factor(day))) +geom_bar(fill ="skyblue") +labs(title ="Number of Clicks per Day",x ="Day of the Month",y ="Number of Clicks") +theme_minimal()
# Identify days with zero clickszero_click_days <- clicks_data %>%filter(clicks ==0) %>%pull(day)print(paste("Days with zero clicks:", paste(zero_click_days, collapse =", ")))
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.
# Example data for priceprice_data <-data.frame(price =c(NA, 0.5, 0.2, NA, 0.05, 1.0, 0.15, 0.3))# Count NA valuesna_count <-sum(is.na(price_data$price))total_count <-nrow(price_data)na_percent <- (na_count / total_count) *100print(paste("Number of NA values in price:", na_count))
[1] "Number of NA values in price: 2"
print(paste("Percentage of NA values in price:", round(na_percent, 2), "%"))
[1] "Percentage of NA values in price: 25 %"
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.
invalid_price_count <-sum(price_data$price <0.1, na.rm =TRUE)print(paste("Number of values of price less than 0.1:", invalid_price_count))
[1] "Number of values of price less than 0.1: 1"
Exercise 4
Insert a code cell that drops all of the rows that contain invalid or NA values for price.
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.
keyword_data <-data.frame(keywords =c("apple", "banana", "appl", "banana", "orange", "bananna", "apple"))# Load necessary librarieslibrary(janitor)# Create a tabyl of the counts of each keywordkeyword_counts <- keyword_data %>%count(keywords) %>%adorn_totals("row")print(keyword_counts)
keywords n
appl 1
apple 2
banana 2
bananna 1
orange 1
Total 7
Exercise 6
Insert a code cell that corrects all the misspellings for keyword, then rerun tabyl to verify.
keyword value
1 data 1
2 date 2
3 datta 3
4 datum 4
5 datta 5
6 date 6
# Correcting misspellings in the 'keyword' columndata <- data %>%mutate(keyword =recode(keyword,"datta"="data","datum"="data"))# Display the corrected dataprint("Corrected Data:")
[1] "Corrected Data:"
print(data)
keyword value
1 data 1
2 date 2
3 data 3
4 data 4
5 data 5
6 date 6
# Rerun tabyl to verifykeyword_tabyl <- data %>%tabyl(keyword)# Display the results of the tabylprint("Tabyl Results:")
[1] "Tabyl Results:"
print(keyword_tabyl)
keyword n percent
data 4 0.6666667
date 2 0.3333333
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.