data science / r
Learn R for Data Science Free: The Tidyverse Route, With a pandas Translation
R is the better first language for some data work and the wrong one for other work. This page sets out which is which, shows the same analysis in R and in pandas side by side, lists exactly what to install and what it costs (nothing), and puts the free books in a reading order.
R was built by statisticians for data analysis, and it shows: data frames, missing values, statistical models and publication-quality graphics are first-class features rather than libraries bolted on. Python is the more general language, with a wider job market around software and machine learning engineering. Plenty of working analysts know both. If your first goal is statistics, research, reporting or visualization, R is a direct path, and the modern way to learn it, the tidyverse, reads closer to English than the older base-R style that gave the language its reputation for being awkward.
01 · THE SAME ANALYSIS TWICE
A five-line tidyverse pipeline and its pandas equivalent
R's built-in teaching dataset mpg (from ggplot2) holds fuel economy figures for 234 car models. The question: for 2008 models, how many cars are in each class and what is the average highway mileage, from best to worst?
library(tidyverse) mpg |> filter(year == 2008) |> group_by(class) |> summarise(cars = n(), mean_hwy = mean(hwy)) |> arrange(desc(mean_hwy)) ggplot(mpg, aes(displ, hwy, colour = class)) + geom_point()
Read the pipe |> as "and then". Each verb takes a table and returns a table, so a pipeline is a recipe you read from top to bottom. The same verbs map onto pandas, which helps if you are coming from the Python guide or planning to learn both:
| Task | R (dplyr) | Python (pandas) |
|---|---|---|
| Keep rows | filter(year == 2008) | df.query("year == 2008") |
| Keep columns | select(class, hwy) | df[["class", "hwy"]] |
| New column | mutate(ratio = hwy / cty) | df.assign(ratio=df.hwy / df.cty) |
| Group and summarize | group_by(class) |> summarise(m = mean(hwy)) | df.groupby("class").agg(m=("hwy", "mean")) |
| Sort | arrange(desc(m)) | .sort_values("m", ascending=False) |
02 · WHEN TO CHOOSE R
R or Python: a decision by task
| Your goal | Lean toward | Reason |
|---|---|---|
| Academic statistics, survey or biomedical analysis | R | R is a long-standing standard in statistics teaching; MIT 18.05 and Harvard's Data Science: Probability course both use it. |
| Reports and figures for publication | R | ggplot2 and Quarto are built for exactly this; R4DS ends with chapters on Quarto. |
| Production machine learning, deep learning | Python | The deep learning frameworks target Python. See the deep learning guide. |
| Scripts that glue systems and serve web apps | Python | General-purpose language with a broader software ecosystem. |
| Not sure yet | Whichever your course or team uses | The concepts transfer; the table above is the proof. |
03 · INSTALL AND COST
What to install, and what is free
Everything needed is free. Install R itself first, then an editor. R4DS recommends R 4.2.0 or later and RStudio 2022.02.0 or later; the current Windows download page on CRAN offers R 4.6.2.
| Option | What you get | Limits to know |
|---|---|---|
| R from CRAN | The language itself | Console only; no editor |
| RStudio Desktop (open source) | The standard R editor, running on Windows 11, macOS 15+ and Linux | The open-source edition is AGPL-licensed; commercial editions exist but are not needed |
| Posit Cloud, Free plan | RStudio in the browser, $0 forever per the plan page | 25 compute hours a month, 1 GB RAM, 1 CPU, 25 projects |
Posit Cloud is useful if you cannot install software on your machine. Its free plan is generous for learning but small for real data. Once a project nears 1 GB of RAM, switch to a local install.
04 · THE FREE LIBRARY
Free books and tutorials, in reading order
Start with an interactive tutorial to get over the syntax, then read R for Data Science, which is the backbone of most learning paths.
- swirl: Learn R, in R ↗Interactive lessons that run inside the R console. Install R, then
install.packages("swirl")and follow the prompts; the swirl site asks for R 3.1.0 or later.swirlstats.com - R for Data Science, 2nd edition ↗By Hadley Wickham, Mine Cetinkaya-Rundel and Garrett Grolemund. The site says it is and will always be free, under a CC BY-NC-ND 3.0 license; print copies are sold separately. It covers visualization, transformation, tidying, import, programming and Quarto.r4ds.hadley.nz
- R4DS chapter 1: data visualization ↗The first working chapter, and the quickest way to see whether R suits you: you make plots before learning the theory.r4ds.hadley.nz
- R4DS chapter 5: data tidying ↗Reshaping data so each variable is a column. The step that makes later analysis simple.r4ds.hadley.nz
- tidyr: tidy data ↗An informal, code-heavy version of the tidy data paper, from the tidyr documentation.tidyr.tidyverse.org
- Hands-On Programming with R ↗Learn the language through three projects: weighted dice, playing cards and a slot machine. Good when you want programming fundamentals more than data analysis.rstudio-education.github.io
- ModernDive: Statistical Inference via Data Science ↗Data visualization, wrangling, regression and statistical inference using R. Licensed CC BY-NC-SA 4.0.moderndive.com
- ggplot2: Elegant Graphics for Data Analysis ↗The ggplot2 book, third edition, once R4DS leaves you wanting more control over graphics.ggplot2-book.org
- The R Graph Gallery ↗Hundreds of charts with reproducible code. Find one close to your idea and change the data.r-graph-gallery.com
- Advanced R ↗For later: how the language itself works, covering names and values, functions, environments and functional programming. Skip it until you have written a few hundred lines.adv-r.hadley.nz
- An Introduction to R (CRAN manual) ↗The official manual. Dense, but the authoritative reference for base R.cran.r-project.org
05 · TRY IT
Reproduce the example, then break it
install.packages("tidyverse"), and paste the pipeline from section 01. Then change it: filter to cyl == 4, group by drv instead of class, or add median(hwy) next to the mean. Finish by reading R4DS chapter 1 and recreating its first plot with your own dataset.|> and use %>% from the magrittr package, which still works. Copying a mix of both styles into one script is fine, but keep one for your own code. R4DS 2e uses the native pipe.06 · FAQ
Frequently asked questions
Should I learn R or Python for data science?
Learn R if your focus is statistics, academic research, reporting or visualization, and Python if you want machine learning engineering or general programming. The data wrangling concepts transfer between them, and many analysts end up using both.
Is R for Data Science really free?
Yes. The second edition is published online at r4ds.hadley.nz, which states that the website is and will always be free under a CC BY-NC-ND 3.0 license. A printed copy is sold separately.
Do I need to pay for RStudio?
No. The open-source RStudio Desktop is free and licensed under the AGPL. If you cannot install software, Posit Cloud has a free plan with 25 compute hours a month and 1 GB of RAM.
What is the tidyverse?
The tidyverse is a collection of R packages for data science that share a design, including dplyr for transforming data, tidyr for reshaping it and ggplot2 for graphics. Learning R this way is easier than starting with base R.