I created a fictional business data .csv file containing very messy data. The raw data was wide with inconsistent units and an outlier. It was not formatted for analysis, so before exploring the relationships, I first tidyed the data. I then calculated profit and found the city with the greatest earnings.
Import Data
Import data from github hosted .csv and glimpse what we are working with.
The data needs to be formatted for analysis. I first cleaned up the column names using the Janitor library. In order to pivot and combine columns, I needed to convert everything to characters and remove the commas. There are also some months that are reported in thousands, so the units of the cost and revenue needed to be standardized. Then I converted from wide to long and fixed the column names.
Code
df_tidy <- df_raw %>%clean_names() %>%# Convert everything to character to prevent mismatches latermutate(across(everything(), as.character)) %>%# Pivot all month columns pivot_longer(cols =-c(location, name),names_to ="measure",values_to ="value" ) %>%# Remove commas and convert to to numericmutate(value =as.numeric(str_remove_all(value, ",")) ) %>%# Extract month name and cost/revenuemutate(month =str_extract(measure, "^[a-z]+"),metric =if_else(str_detect(measure, "cost"), "cost", "revenue"),# Scale cost values if given in thousandsvalue =if_else(metric =="cost"& value <=100, value *1000, value)) %>%# Pivot into separate `cost` and `revenue` columnspivot_wider(id_cols =c(location, name, month),names_from = metric,values_from = value ) %>%# Clean up month formatting and reordermutate(month =str_to_title(month),month =factor(month, levels =c("Jan", "Feb", "March", "April", "May", "June")) ) %>%arrange(location, month)head(df_tidy)
# A tibble: 6 × 5
location name month cost revenue
<chr> <chr> <fct> <dbl> <dbl>
1 Boston Bobby Jan 29000 50000
2 Boston Bobby Feb 50000 52000
3 Boston Bobby March 40000 48000
4 Boston Bobby April 50000 53000
5 Boston Bobby May 25000 6150000
6 Boston Bobby June 45000 44000
Analysis
First we subtract the costs from revenue to calculate profit and summarize that in a table. This allows us to see that there is an outlier in the data, likely a mistake. The Boston revenue for May was $6,150,000, suggesting a typo. When that is corrected to $61,500, the data looks much more reasonable.
ggplot(df_tidy, aes(x = month, y = profit, color = location, group = location)) +geom_line(linewidth =1.2) +geom_point(size =2.5) +scale_y_continuous(labels = scales::dollar) +theme_minimal() +labs(title ="Monthly Profit by Location",x ="Month",y ="Profit",color ="Location" )
Discussion and Next Steps
After the Boston outlier was identified and corrected, Boston still remained the second best performer. However, Dallas just out performed Boston with a 24% profit margin compared to Boston’s 21%. Both the cities owe their status to one outstanding month, rather than steady high profits. Chicago shows volatile month to month performance, while the rest of the cities earned more modestly, but also more consistently.
Next steps with this data could be to aggregating all costs/revenues to see the national performance over the 6 months.