versionprint(.packages())
Problem Set 3: Data Processing with R
Link to the description of the supermarket sales data available in the Supermarket Sales Data Overview.
Instructions
Write an R script that completes the following tasks. Make sure you follow best practices, including commenting on your code.
Note on Log File Output:
When running your R script, you may notice that some lines in your log file appear with “[TRUNCATED]” comments. This happens when the output exceeds R’s default display limits.
To minimize truncation, add line breaks before the grey vertical line in the Source Pane to keep lines within the display limit. Additionally, avoid printing large datasets directly—use glimpse() or head() instead of printing entire data frames.
Response Submissions: For the responses to the questions that require explanations, please post your responses as text on your Google site. Be sure to clearly indicate which question each response refers to.
Tasks
- Read in the dataset
supermarket_sales.csv
usingread_csv()
.
- Question: What packages do you need to have installed before loading in the data using this command?
- Write your response as text on your Google site, indicating which question you are answering.
- Calculate the total value of the sale using the
unit_price
andquantity
columns.
- Create a new column called
subtotal
. - Verify that the value labeled
tax_5_percent
is indeed 5% of thesubtotal
by creating a new variable calledtax_verify
. - Assign the object to a new dataframe.
Filter the dataset to create a dataframe containing only the sales from the product line “Food and beverages”.
Select relevant columns: Create a dataframe that contains only the columns
city, product_line, unit_price, quantity, total, rating
for sales in “Food and beverages”.Sort the dataframe by
quantity
in descending order.Calculate median sales by payment type using the appropriate commands.
Suppose you are asked to develop a new performance indicator for the company. You decide that “rating per unit price” or “rup” would be a helpful metric.
- Compute the rating per unit price for each transaction by creating a new column called
rup
. - Explain: What insights might this performance indicator provide to decision-makers in the company?
- Write your response as text on the Google site, clearly indicating which question you are answering.
- Summarize
rup
andunit price
by product line:
- Compute the
mean(rup)
andmean(unit_price)
grouped by product line.
- Print the summarized dataframe:
- Use the
print()
function to display the results - Explain: What conclusions can you draw from your analysis?
- Write your response as text on the Google site, clearly indicating which question you are answering.
- Include the following lines of code to ensure that you have loaded all the necessary packages for this assignment:
Save your R script as
problem_set_3.R
.Generate a log file to show that it ran successfully in R. Use the
sink()
command to capture both the code and its output. Run the script and store the log inproblem_set_3.log
. Below is a hint for structuring this:
sink("problem_set_3.log") # Start capturing output
source("problem_set_3.R", echo = TRUE) # Run your script and show commands + output
sink() # Stop capturing output
How to Submit
- Create a Webpage on Your Google Site:
- Title the new webpage Problem Set 3.
- This page should contain an R script log file that responds to all questions of the problem set.
- Submit the Link to Your Google Site Page:
- Copy the URL of your Google Site webpage for Problem Set 3.
- Submit this link via Canvas.