R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
226: Timed Mock Assessment on Core R and Tidyverse
Alright, we've spent the last few dozen lessons building up your toolkit. Now, we're at the part I always dread but know is necessary: the timed mock assessment. Instead of just handing you a PDF of questions and saying "good luck," I want to walk you through how I approach these under pressure. I've put together a messy dataset of global coffee production—real-world style, which means it's formatted poorly—and I'm going to set a timer for 20 minutes to extract three specific insights.
Wrestling with the "Wide" Data Trap
First things first. I've got this `coffee_data` frame. Let's see what I'm working with.
# I'll start by peeking at the structure
head(coffee_data)
# Output:
# Country 2020 2021 2022 Region
# Brazil 3500 3200 3800 Americas
# Vietnam 1800 1900 1700 Asia
# Ethiopia 400 420 410 Africa
Immediately, I see the trap. The years are columns. If the assessment asks me to find the average growth rate across all years, I can't do that easily while the data is "wide." I'm tempted to just write a loop, but this is a Tidyverse assessment—I need to be faster. I'll pivot this immediately.
library(tidyverse)
# Attempt 1: Just pivot
coffee_long <- coffee_data %>%
pivot_longer(cols = 2:4, names_to = "year", values_to = "production")
head(coffee_long)
# Wait, the 'year' column is a character string now.
# I can't do math on "2020".
That's a classic mistake. In a timed test, it's easy to forget that `pivot_longer` doesn't automatically know that your column names are actually numbers. I'll fix that on the fly using `mutate`.
coffee_long <- coffee_data %>%
pivot_longer(cols = 2:4, names_to = "year", values_to = "production") %>%
mutate(year = as.numeric(year))
The Joining Headache
Now, the mock prompt asks for the total production grouped by "Economic Tier" (High, Middle, Low), but that info isn't in my main table. It's in a separate `country_meta` dataframe. Let's try a simple join.
# Trying to merge the data
final_set <- coffee_long %>%
left_join(country_meta, by = "Country")
# Checking for NAs
sum(is.na(final_set$economic_tier))
# Output: 12
Twelve NAs? That's a red flag. I know every country in my production list should have a tier. I'll check the original `country_meta` and... there it is. Some countries are listed as "Viet Nam" in the meta-table but "Vietnam" in the production table. This happens constantly in real-world data. I don't have time to manually rename everything, so I'll use `str_replace` to standardize the most common culprit before re-joining.
country_meta <- country_meta %>% mutate(Country = str_replace(Country, "Viet Nam", "Vietnam")) # Now try the join again final_set <- coffee_long %>% left_join(country_meta, by = "Country")Racing the Clock with Aggregation
The final requirement is a summary table showing the year-over-year growth percentage per region. I'm down to my last five minutes. I need to calculate the difference between the current year and the previous year. I'll use `lag()`, but I have to be careful—if I don't group by region first, I'll accidentally subtract Brazil's 2020 production from Ethiopia's 2021 production.
growth_report <- final_set %>% group_by(Region) %>% arrange(year) %>% mutate(prev_prod = lag(production)) %>% mutate(growth = (production - prev_prod) / prev_prod * 100) %>% ungroup() # Let's see if it worked filter(growth_report, Region == "Americas")There it is. The `lag()` function is a lifesaver here. If I had tried to do this with base R indexing, I'd probably still be staring at the screen. By chaining these Tidyverse verbs, I've turned a messy, wide dataset into a clean growth report in under 20 minutes. The key isn't knowing every function perfectly; it's knowing how to pivot, join, and group without getting tangled in the syntax.
📋 Practical Task
Exercise: Cleaning and Analyzing the Brazilian Coffee Export Surge
You have been provided with two dataframes: `export_data` (containing columns `Country`, `2015`, `2016`, `2017`, `2018`, `2019`, and `2020` representing export volumes) and `country_details` (containing `Country` and `Export_Tax_Bracket`).
Write a script to perform the following tasks:
- Transform the `export_data` from wide format to long format, ensuring the year is stored as an integer.
- Join the data with `country_details`, handling the case where "Brazil" is misspelled as "Brasil" in the `country_details` table.
- Calculate the total export volume for each `Export_Tax_Bracket` for the year 2020.
- Filter the final result to show only brackets that exported more than 1,000,000 units in 2020.
There are no comments for now.