Skip to Content
Course content

39: Comparing R and Python for Data Analysis

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've spent a good chunk of my career jumping between R and Python. Usually, when someone asks me "which one is better," I tell them that's the wrong question. It's more about where you are in the lifecycle of a project. To show you what I mean, I'm going to walk through a quick analysis of some weather data I've got here—hourly temperature readings for a few different cities—and I'll "think out loud" about how the experience differs between the two languages.

The "Quick and Dirty" Wrangling Phase

I'll start in R because I'm in "exploration mode." I want to know the average temperature per city, but only for the hours between 8 AM and 12 PM. In R, my brain immediately goes to the tidyverse. I'll just pipe it together.

# Let's see if this works as expected
weather_summary <- weather_data %>%
  filter(hour >= 8 & hour <= 12) %>%
  group_by(city) %>%
  summarize(avg_temp = mean(temp, na.rm = TRUE))

print(weather_summary)

That felt seamless. Now, if I were doing this in Python with pandas, I'd be writing something like df[(df['hour'] >= 8) & (df['hour'] <= 12)].groupby('city')['temp'].mean(). Now, it's not "harder," but the syntax is denser. When I'm debugging in R, I can comment out one line of the pipe and see exactly where the data transforms. In Python, nested brackets and method chains can sometimes feel like a puzzle when they throw a KeyError at you. R feels like it was built by a statistician for a statistician; Python feels like it was built by a software engineer who decided to add data tools.

The Visualization Friction Point

Now I want to visualize this. I want a boxplot of temperatures by city, colored by the season. This is where I usually find the biggest "feel" difference.

# Let's try to get this plot up quickly
library(ggplot2)

ggplot(weather_data, aes(x = city, y = temp, fill = season)) +
  geom_boxplot() +
  theme_minimal() +
  labs(title = "Temperature Distribution by City")

The "Grammar of Graphics" approach in ggplot2 is, in my opinion, a superpower. I just describe the mapping—x is city, y is temp, color is season—and R handles the legend, the axes, and the grouping automatically. If I tried this in Python using matplotlib, I'd be writing a loop or manually managing the colors for the seasons. seaborn makes it easier, but you're still essentially using a wrapper around a plotting library that wasn't originally designed for data frames. I find I "iterate" faster in R because the visualization is a first-class citizen.

Where the Tide Turns

But here is where I start to feel the limitation. Let's say I want to take this analysis and turn it into a real-time dashboard that pulls data from a live API every ten minutes and sends an alert to Slack if a temperature drops too low. Suddenly, I'm not "analyzing" anymore—I'm "engineering."

In R, I'd be looking at Shiny, which is fantastic for dashboards, but integrating it into a larger corporate microservice architecture? That's a headache. In Python, I'd just use FastAPI or Flask, use the requests library to hit the API, and I'm done. Python's ecosystem for "everything else"—web servers, database connectors, cloud deployment—is miles ahead of R.

So, the pattern I've settled into over the years is this: I use R to find the answer. I use Python to build the product that delivers the answer. If I'm just trying to understand a dataset, I don't want to fight with pandas indexing; I want dplyr. But if I'm writing code that needs to run on a server for three years without me touching it, I'm switching to Python.




📋 Practical Task

Analysis Workflow Translation: The Air Quality Set

You have been given a dataset air_quality_df containing columns: location, pm25 (particulate matter), and timestamp.

Your task is to simulate the "Exploration" phase in R. Write a script that performs the following sequence using the pipe operator:

  • Filter the data to only include records where pm25 is greater than 12.0.
  • Group the data by location.
  • Calculate the median pm25 for each location.
  • Create a basic ggplot2 bar chart showing the median pm25 per location.

Once you've written the R code, add a comment at the bottom of your script explaining one specific part of the ggplot2 syntax that you believe would be more verbose to implement in a standard Python matplotlib workflow.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.