R
Lộ trình phát triển toàn diện R theo tiêu chuẩn quốc tế nilbuild/developer-roadmap
Hướng dẫn từng bước từ nền tảng đến chuyên sâu giúp bạn làm chủ R. Tích hợp tài liệu lý thuyết, bài viết thực chiến, video tham khảo và bài tập lập trình trực tiếp trên IDE.
Nền Tảng & Khái Niệm Cốt Lõi
Giai đoạn 1 tập trung hoàn thiện 26 chủ đề then chốt.
Aesthetic Mappings
Aesthetic mappings connect columns in your data to visual properties of a plot, like position, color, size, or shape. Writing `aes(x = year, y = revenue, color = region)` tells ggplot2 to plot year against revenue and color each point by region. Choosing the right aesthetics for the right variables makes the difference between a clear plot and a confusing one.
APIs
Web APIs let you request data over the internet from another service, typically returning it in JSON format. Fetching data from an API in R usually means making an HTTP request with a package like httr, then parsing the response. This combination of requesting and parsing is essential for working with any modern data source that exposes an API rather than a static file.
Apply Family
The apply family is a set of base R functions that apply another function to every element of a vector, list, or data structure, replacing many uses of explicit loops. `lapply()` always returns a list, `sapply()` tries to simplify the result into a vector or matrix, and `vapply()` works like `sapply()` but requires specifying the expected output type in advance. These functions predate the tidyverse's purrr package, which offers similar functionality with more consistent behavior.
Arguments & Defaults
Function arguments are the inputs a function accepts, and default values let you specify what an argument should be if the caller doesn't provide one. Writing `function(x, na.rm = FALSE)` means `na.rm` defaults to `FALSE` unless the caller overrides it. R also supports `...` to let a function accept a variable number of additional arguments, often passed through to another function inside.
Arrays
An array generalizes a matrix to more than two dimensions, storing values of a single type across any number of dimensions you specify. Arrays are less common in everyday data analysis than vectors or data frames, but they appear in specialized numerical and scientific computing contexts. R indexes into them using the same bracket notation used for vectors and matrices, just with an extra dimension for each additional axis.
Base R Plotting
Base R includes plotting functions like `plot()`, `hist()`, and `boxplot()` that don't require any additional package. They're quick to use for a fast look at data and require no setup. Base R plots are generally less polished and more manual to customize than ggplot2, since you build them up layer by layer using separate function calls.
Basic Syntax
This section covers the fundamental building blocks of writing R code: variables, operators, comments, and basic control structures. It's the vocabulary you need before anything more complex makes sense.
Best Practices
Good data visualization practice means choosing chart types and design choices that communicate data accurately rather than misleadingly. Common anti-patterns include truncated axes that exaggerate differences and color choices that aren't accessible to colorblind viewers. A technically correct chart can still mislead its audience if these principles are ignored.
Big Data Tools
This section covers working with datasets too large or too slow to handle comfortably with standard R data manipulation tools. It includes techniques for parallelizing computation and connecting to distributed processing systems.
Casting Types
Casting converts a column to the correct type, such as turning character `"42"` into the numeric value `42`. Data often arrives in the wrong type for analysis, especially numbers or dates stored as plain text. Getting the types right early avoids confusing errors later when a function expects a number but receives text instead.
Character
Character is R's type for text, written inside single or double quotes, like `"hello"`. R provides many built-in and package functions for searching, splitting, and reformatting text stored this way. Character data shows up constantly in real datasets: names, categories, free-text responses, file paths.
Comments
Comments start with `#` and are ignored when R runs the code, letting you leave notes explaining what a piece of code does or why. They don't affect how a script executes, but they make code easier to understand later, whether for someone else or for yourself after time has passed. Good commenting habits pay off especially once scripts grow beyond a few lines.
Common Plots
Common plot types include line plots for trends over time, bar plots for comparing quantities across categories, histograms for showing the distribution of a single numeric variable, and boxplots for summarizing spread and outliers. Each type suits a different kind of question about the data. Learning when to reach for which one is as important as knowing the syntax to produce it.
Complex
Complex numbers combine a real and imaginary part, written in R as something like `3+2i`. They come up in specialized mathematical work, such as signal processing, but most day-to-day data analysis never touches them directly. R supports them fully as a native type, including arithmetic and functions like `Mod()` and `Conj()`.
Conditional Statements
Conditional statements let code branch based on whether something is true or false, using `if`, `else if`, and `else`. R evaluates the condition, runs the matching block, and skips the rest. R also offers `ifelse()` and dplyr's `case_when()` for applying conditional logic across an entire column of data at once.
Correlation
Correlation measures the strength and direction of a linear relationship between two numeric variables, expressed as a single number between negative one and positive one. A value near positive one means the variables tend to increase together, and near zero means little linear relationship exists. Correlation does not imply causation, and it can miss real relationships that aren't linear in shape.
CRAN
CRAN, the Comprehensive R Archive Network, is R's official package repository, hosting tens of thousands of vetted packages. Packages published on CRAN go through basic automated checks before release, giving them a baseline level of reliability. It's the default source `install.packages()` pulls from, and most tutorials assume packages come from here unless stated otherwise.
Creating Variables
Creating a variable stores a value under a name so you can reuse it later, most commonly using the `<-` assignment operator, though `=` also works in most contexts. Once assigned, a variable holds its value until you reassign or remove it. This is one of the very first things you do in any R script.
CSV
CSV files store tabular data as plain text with values separated by commas, making them one of the most universal formats for exchanging data between tools. Base R can read them with `read.csv()`, though readr's `read_csv()` is faster and handles more edge cases well, such as inconsistent column types or unusual encodings. Reading them correctly means paying attention to details like headers and how missing values are represented.
Dashboards
Dashboards present data and analysis interactively, typically through a web page with charts, filters, and controls a viewer can adjust themselves. They're built either through general business intelligence tools or R-native frameworks, depending on the audience and existing tooling in an organization. This section covers both routes for building them out of R work.
Data Cleaning
This section covers fixing messy, inconsistent, or incomplete data before it's ready for analysis. It includes converting types, cleaning text, handling missing values, and detecting outliers.
Data Frames
A data frame is R's standard structure for tabular data, organized into rows and columns where each column can hold a different type but all values within a column share the same type. It's the structure you'll use for almost any real dataset loaded into R, whether from a CSV file, a database, or an API. Nearly every data manipulation package in R, including dplyr, is built around working with data frames or their variants.
Data Manipulation
This section covers the core tasks of reshaping and transforming data once it's already loaded into R: filtering, grouping, joining, and more. Most real analysis work happens here, between importing raw data and producing final results.
Data Structures
This section covers the containers R uses to hold data: vectors, lists, and the various tabular formats. Nearly everything you do in R involves working with one of these structures.
Data Tables
A data table is the structure provided by the data.table package, built specifically for fast, memory-efficient operations on large datasets. It extends the data frame with a more concise syntax and internal optimizations that make filtering, grouping, and joining noticeably faster on big data. Many R users learn dplyr first and pick up data tables specifically when performance on large datasets becomes a bottleneck.
Data Types
This section covers the basic kinds of values R works with: numbers, text, and logical values. Every piece of data in R falls into one of these categories, and knowing them shapes how you write and debug code.
Kỹ Năng Trọng Tâm & Thực Hành
Giai đoạn 2 tập trung hoàn thiện 26 chủ đề then chốt.
Debugging
Debugging in R involves finding out why code isn't behaving as expected, using tools like `browser()`, which pauses execution at a specific line so you can inspect variables, and `traceback()`, which shows the sequence of function calls leading up to an error. RStudio and Positron both provide visual debugging tools built on top of these, including breakpoints you can set by clicking in the editor. Print statements remain a simple and effective debugging tool for many everyday issues.
Descriptive Statistics
Descriptive statistics summarize the basic characteristics of a dataset: central tendency, spread, and shape. They form the foundation for almost every deeper statistical technique, since you need to understand a variable's basic behavior before modeling relationships involving it. R's base `summary()` function calculates several of these at once for a quick overview.
Distribution Analysis
Distribution analysis looks at how values in a dataset are spread across their range, most commonly using histograms and boxplots. A histogram groups values into bins and shows how many observations fall into each, revealing shape characteristics like skewness. A boxplot summarizes a distribution's median, quartiles, and potential outliers in a single compact visual.
dplyr
dplyr provides a small set of verbs for the most common data manipulation tasks: filtering rows, selecting columns, arranging order, and summarizing groups. These verbs are designed to read almost like plain English, making dplyr code easier to follow than equivalent base R code. It's the core data manipulation package within the tidyverse.
Dropping vs. Imputing
Once missing values are found, you have to decide whether to drop rows containing them, which is simple but can lose real information, or impute them, replacing missing values with an estimate like the column mean. The right choice depends heavily on why the data is missing and how much of it there is. Dropping too aggressively can bias results if the missingness isn't random.
Encoding Categories
Encoding categories turns text labels into R's factor type, giving categorical data the structure needed for modeling and certain visualizations. It also involves deciding on the order categories should follow, since default alphabetical ordering isn't always meaningful. This step usually comes after the raw text has already been cleaned and standardized.
Error Handling
This section covers how to catch and respond to problems in your code instead of letting them crash your script outright. `tryCatch()` lets you run code that might fail and specify what should happen if it does, such as logging a message or returning a default value. This matters for any code that depends on unreliable inputs, like reading files or calling an API.
Excel
Excel files store data in a binary or XML-based format that differs from plain text files, often including multiple sheets and formatting within a single file. R doesn't read them with base functions, so packages like readxl are needed to extract the underlying data into a usable data frame. Reading Excel data means deciding which sheet to read and whether the first row contains headers.
Exploratory Data Analysis
This section covers the first, applied pass at understanding a new dataset: summarizing it numerically and visually before any deeper modeling. It's usually the step right after data cleaning and right before serious statistical analysis.
Faceting & Themes
Faceting splits a single plot into a grid of smaller plots, one for each level of a categorical variable, making it easy to compare patterns across groups side by side. Themes control the overall visual styling of a plot, like background color, gridlines, and font sizes. Together they cover much of the difference between a rough exploratory plot and one polished enough to share.
Factors
A factor represents categorical data, storing a fixed set of possible values called levels, such as "low", "medium", "high". Internally, R stores factors as integers mapped to those level labels, which is more memory-efficient than storing the same category repeatedly as text. Getting factor levels and their order right is a common source of subtle bugs, especially when merging or filtering categorical data.
Filter, Query, Arrange
Filtering keeps rows matching a condition, querying more broadly covers selecting the specific subset of data you need, and arranging sorts rows by one or more columns. In dplyr, these map to `filter()` and `arrange()`, both designed to read almost like plain English. Together they cover a large share of everyday data wrangling needs.
forcats
forcats provides tools for working with factors, R's data type for categorical variables, making common tasks like reordering levels or lumping rare categories together more straightforward than base R. Functions like `fct_reorder()` let you control the order categories appear in in plots and tables, which matters since default factor ordering is often alphabetical rather than meaningful. It's especially useful when preparing categorical data for visualization.
Forward / Backward Fill
Forward and backward fill are common in time series data, carrying the last known value forward or the next known value backward to fill gaps. This assumes a value likely stayed the same or is reasonably close to nearby recorded values. It's a simpler alternative to statistical imputation, often used when data is missing due to something like a sensor briefly going offline.
Functions
This section covers writing and using your own functions in R: how they accept inputs, return values, and interact with the rest of your code. Functions are the main way to package logic so it can be reused instead of repeated.
Geospatial Analysis
Geospatial analysis works with data tied to specific locations on Earth, using specialized structures and tools for handling coordinates, boundaries, and spatial relationships. It covers both vector data, like points and polygons, and raster data, like satellite imagery. This is a distinct application area requiring its own set of packages beyond standard data manipulation tools.
ggplot2
ggplot2 is the tidyverse's plotting package, built around the grammar of graphics, which describes any plot as a combination of data, a coordinate system, and layered elements mapped to aesthetics like position or color. Instead of choosing a fixed chart type, you build a plot by combining these pieces, which is why the same approach can produce scatterplots, bar charts, or complex faceted layouts. It's the standard tool for data visualization in modern R.
ggplot2
ggplot2 is a data visualization package that follows the grammar of graphics, a system for describing and building graphs by combining independent components. It allows users to create complex plots by layering elements such as data, coordinate systems, and visual mappings. By providing a consistent and structured approach to plotting, it enables the creation of publication-quality visualizations through a series of intuitive, additive commands.
Grammar of Graphics
The grammar of graphics is the conceptual framework ggplot2 is built on, describing any plot as a combination of data, a coordinate system, and layered geometric elements mapped to aesthetics. Instead of choosing a fixed chart type, you build a plot by combining these pieces, which is why the same underlying approach can produce scatterplots, bar charts, or complex layouts using consistent syntax. Understanding this grammar is what makes ggplot2 code readable and predictable once you know the pattern.
Group By & Summarize
Grouping splits a data frame into groups based on one or more columns, and summarizing then collapses each group down to a single summary row, such as a group's average or count. Together they replace the need for manual loops when calculating statistics per category, like average sales per region. This pattern is one of the most commonly used in all of data analysis.
Hypothesis Testing
Hypothesis testing provides a formal framework for deciding whether an observed pattern in data is likely real or could plausibly have occurred by chance alone. It starts with a null hypothesis, typically stating there's no effect, and calculates a p-value representing how surprising the observed data would be if that null hypothesis were true. Common tests include the t-test for comparing means and the chi-square test for categorical relationships.
igraph
igraph is the standard R package for building, analyzing, and visualizing network graphs. It provides tools for calculating centrality measures, detecting communities, and computing paths between nodes, covering most of the core graph theory operations needed. It's the foundation many other network analysis tools in R, including tidygraph, build on top of.
Importing Data
This section covers getting data from outside sources into R, whatever form it originally comes in. Different sources, flat files, spreadsheets, web pages, and APIs, each need a different approach to load correctly.
Installing Packages
Installing packages adds functionality to R beyond what comes built in, using the base function `install.packages()` to download and install from CRAN. Running `install.packages("dplyr")`, for example, fetches the dplyr package and its dependencies. This is the most basic and common way to extend R with new capabilities.
Installing R
Installing R involves downloading the base software environment from the Comprehensive R Archive Network (CRAN) and executing the installer for your specific operating system. This process sets up the core language engine, allowing you to run R scripts, manage packages, and perform statistical computations directly on your computer. Once the installation is complete, you can interact with the language through the default console or by using an integrated development environment like Positron or RStudio.
Integer
Integers are whole numbers stored more compactly than numeric values, created by appending an `L` to a number, like `5L`. They matter when memory efficiency counts, since integers take up less space than numeric doubles. Most everyday R code doesn't need to worry about the distinction, since R converts between the two automatically in most operations.
Kiến Trúc Nâng Cao & Tối Ưu
Giai đoạn 3 tập trung hoàn thiện 26 chủ đề then chốt.
Interactive Plots
Interactive plots let viewers hover over points for details, zoom into a region, or toggle categories on and off, rather than viewing a fixed, static image. They work particularly well in dashboards, reports viewed in a browser, or any context where the audience benefits from exploring the data themselves. They aren't useful for print or contexts where the output needs to be a fixed image.
What is R
R is a programming language built for statistical computing and data analysis. Bell Labs statisticians created its predecessor, S, in the 1970s, and R emerged in the 1990s as an open-source implementation of similar ideas. It comes with built-in support for vectors, data frames, and statistical functions, so tasks like regression or plotting a distribution take just a line or two of code. Universities, pharmaceutical companies, and data science teams use it heavily for analysis, visualization, and research reproducibility.
IQR
The IQR method for detecting outliers flags values that fall well below or above the interquartile range, the range between the 25th and 75th percentile of the data. It's a robust approach, less sensitive to extreme values than methods based on the mean and standard deviation. It's commonly visualized directly through a boxplot's whiskers.
isnull, isna
Detecting missing values in R relies on `is.na()`, since direct comparison like `NA == NA` doesn't return `TRUE` the way you might expect. This function returns a logical vector marking which values are missing, which can then be summed to count them or used to filter rows. It's the starting point before deciding how to handle any missing data found.
Joins
Joins combine two data frames based on matching values in one or more shared columns, similar to joins in SQL. An inner join keeps only rows with matches in both tables, a left join keeps all rows from the first table and fills in matches where available, and a full join keeps all rows from both regardless of matches. Choosing the right join type is essential whenever data is spread across multiple related tables.
JSON
JSON is a structured text format built around nested key-value pairs, commonly returned by web APIs. Parsing it in R usually means using a package like jsonlite to convert the response into R data structures. Because JSON's nested structure often doesn't map directly onto a flat data frame, some restructuring is usually needed afterward to get it into a usable tabular shape.
leaflet
leaflet creates interactive, web-based maps in R, built on top of the popular Leaflet.js JavaScript library. Unlike static maps, leaflet maps let viewers pan, zoom, and click on individual features to see more detail. It integrates well with Shiny apps, letting map interactions trigger updates elsewhere in a dashboard.
Linear Regression
Linear regression models the relationship between a numeric outcome and one or more predictor variables, fitting a straight line, or hyperplane with multiple predictors, that best describes the relationship. R's `lm()` function fits linear models with a simple formula syntax, like `lm(sales ~ advertising)`. Checking the model's assumptions, like linearity and constant variance of the errors, matters for trusting its results.
Lists
A list is a flexible container that can hold elements of different types and lengths in a single object, unlike a vector, which requires uniform type. A single list might hold a character string, a numeric vector, and a data frame all at once. Accessing elements typically uses double brackets, `[[ ]]`, to pull out a single element, versus single brackets, `[ ]`, which return a sublist.
Logical
Logical values represent true or false, written as `TRUE` and `FALSE`. They result from comparisons like `5 > 3` and drive conditional logic throughout R code, such as `if` statements and filtering rows in a data frame. Logical values can also be summed directly, since R treats `TRUE` as 1 and `FALSE` as 0 in numeric contexts.
Logistic Regression
Logistic regression models a binary outcome, like whether an event happens or not, as a function of one or more predictor variables. It predicts the probability of the outcome falling into one category, using the `glm()` function with a logistic link in R. It's one of the most widely used models for classification problems, both in traditional statistics and as a baseline in machine learning.
Loops
A `for` loop repeats a block of code once for each element in a vector or list. A `while` loop repeats as long as a condition stays true, useful when you don't know in advance how many iterations you'll need. R loops are generally slower than vectorized alternatives, so experienced R users often reach for functions like `sapply()` or dplyr verbs where possible.
lubridate
lubridate makes working with dates and times considerably easier than base R alone. Functions like `ymd()`, `mdy()`, and `dmy()` parse dates written in different common formats without requiring a manually specified format string. It also provides clear tools for date arithmetic using durations, periods, and intervals, each handling calendar quirks like leap years slightly differently.
Machine Learning
This section covers building predictive and pattern-finding models from data, using R's modern modeling frameworks. It builds on the statistical foundations covered earlier in the roadmap.
Map Functions
Map functions apply a function to every element of a list or vector and collect the results, avoiding the need for an explicit loop. purrr's `map()` always returns a list, while variants like `map_dbl()` return a vector of a specific type, making the expected output explicit. This becomes especially useful for iterating over nested data, such as running the same model on many subsets of a dataset at once.
Matrices
A matrix is a two-dimensional grid of values, all of the same type, arranged in rows and columns. Matrices support standard linear algebra operations like multiplication, transposition, and inversion, making them the natural structure for mathematical computation in R. Unlike a data frame, every cell in a matrix must share the same underlying data type.
Mutate & Transform
Mutating adds new columns or changes existing ones in a data frame, calculating values across every row based on an expression you specify. Multiple new columns can be created in a single step, with later columns able to reference ones created earlier in the same step. This is one of the most frequently used data manipulation operations, since almost every analysis involves deriving new variables from existing ones.
Network Analysis
Network analysis studies relationships between entities, represented as a graph of nodes connected by edges, such as people connected by friendships or web pages connected by links. It covers identifying influential nodes, detecting clusters or communities within a network, and visualizing the overall structure. This is a distinct application area from typical tabular data analysis, since relationships between records matter as much as the records themselves.
NLP
NLP, or natural language processing, covers techniques for analyzing and extracting meaning from text data, such as tokenizing, measuring sentiment, or discovering topics across a collection of documents. R has a mature set of packages for this kind of work, ranging from tidyverse-friendly tools to faster, more specialized libraries for large text corpora. It's a distinct application area combining string handling with statistical and sometimes machine learning techniques.
Numeric
Numeric is R's default type for numbers with decimal points, such as `3.14` or `2.0`, stored internally as double-precision floating point values. Almost all arithmetic in R produces numeric results unless you explicitly ask for integers. This is the type used for the vast majority of everyday calculations.
Operators
Arithmetic operators handle basic math, like `+`, `-`, `*`, and `/`. Relational operators compare values and return a logical result, like `==` and `<`. Logical operators combine or invert logical values, using `&` and `|` for element-wise comparisons. These form the backbone of nearly every calculation and condition you write in R.
Overplotting Techniques
Overplotting happens when too many data points overlap on a plot, making it hard to see the true density or pattern, especially common with large datasets. Jittering adds small random noise to point positions to spread out overlapping points slightly. Transparency, adjusted through the alpha setting, lets overlapping points show through each other rather than fully obscuring one another.
pak
pak is a newer package installer for R designed to replace `install.packages()` with something faster and more reliable. It parallelizes downloads, caches packages locally so reinstalls are quicker, and gives clearer error messages when a dependency conflict occurs. Many R developers now use it as their default installer instead of base R's built-in function.
Parallel Computing
Parallel computing splits a computational task across multiple processor cores at once, rather than running everything sequentially, cutting the total time for tasks that can be broken into independent pieces. R provides tools like the parallel package's `parLapply()` for distributing work across a cluster of R processes. Not every task benefits from this, since the overhead of splitting work and combining results can outweigh the benefit for small or fast tasks.
Parsing Dates
Parsing dates handles the extra complexity of date formats, since something like "01/02/2024" could mean different things depending on locale conventions. Getting the format specification right, or using a package like lubridate that guesses common formats automatically, avoids silently misreading dates. This matters especially when working with data from different countries or systems that don't agree on date conventions.
Pipe
The pipe operator takes the result on its left and passes it as the first argument to the function on its right, letting you chain several operations in a readable, top-to-bottom sequence. R now has two common versions: the native pipe `|>`, built into base R since version 4.1, and the older magrittr pipe `%>%`, which predates it and is still common in existing code. Writing `data |> filter(x > 0) |> summarize(mean(x))` reads as a sequence of steps rather than a nested jumble of function calls.
Hệ Sinh Thái & Triển Khai Thực Tế
Giai đoạn 4 tập trung hoàn thiện 26 chủ đề then chốt.
plotly
plotly creates interactive charts that let viewers hover, zoom, and filter directly in the browser. It can convert many existing ggplot2 plots into interactive versions with a single function call, or build interactive charts directly with its own syntax. It's the most common package in R for adding this kind of interactivity to a plot.
Positron
Positron is a newer IDE from Posit, built on the same underlying technology as VS Code but tailored for data science work in R and Python. It combines a modern, extensible editor with the data-focused features R users expect, like a variables pane and integrated plots. It's becoming the default recommendation for new R and data science setups.
Power BI / Tableau
Power BI and Tableau are business intelligence tools for building dashboards, connecting to data sources and creating interactive visualizations largely through a visual interface rather than code. R can connect to both, either as a data source feeding into them or, in Power BI's case, running R scripts directly inside the tool for custom visuals or calculations. They're common in organizations where dashboards need to be shared with non-technical stakeholders through a standard business tool.
Probability Distributions
A probability distribution describes how likely different outcomes are for a random variable, such as the normal distribution or the binomial distribution for counting successes in repeated trials. R provides functions for common distributions following a consistent naming pattern: `d` for density, `p` for cumulative probability, `q` for quantiles, and `r` for random generation. Understanding which distribution reasonably describes your data underlies many statistical tests and models.
purrr
purrr applies a function to each element of a list or vector and collects the results, similar in spirit to base R's apply family but with more consistent, predictable behavior. Its `map()` function always returns a list, while variants like `map_dbl()` return a vector of a specific type, making the expected output explicit. It fits naturally with the pipe operator and tidyverse style, letting you iterate without writing explicit loops.
quanteda
quanteda is a faster, more powerful text analysis package built for working with large corpora of documents, offering more advanced features than tidytext for tasks like document-feature matrices and topic modeling. It trades some of tidytext's tidyverse-native simplicity for more performance and specialized functionality. It's typically reached for once text analysis needs outgrow what tidytext comfortably handles.
Quarto
Quarto is a publishing system that renders documents combining text, code, and code output into formats like HTML, PDF, or Word. It works across multiple languages, including R, Python, and Julia, and is positioned as the modern successor to R Markdown. Posit actively develops and promotes it as the current default for literate programming in R.
R Dates & Times
Base R represents dates and times using specific classes: `Date` for calendar dates and `POSIXct` for date-times that include a time component. These classes let you do arithmetic directly, like subtracting two dates to get the number of days between them. Parsing dates correctly requires knowing the format they were written in, since ambiguous formats can be misread.
R Markdown
R Markdown documents mix Markdown-formatted text with chunks of R code, rendering into HTML, PDF, or Word output with the code's results embedded directly. It predates Quarto and was the standard tool for this kind of work for many years, so a large amount of existing documentation and tutorials still use it. Quarto has since become the recommended tool for new projects, but R Markdown remains fully functional and widely used.
R vs Python for Data Work
R and Python solve overlapping problems but come from different roots. R grew out of statistics departments, so its core language and packages treat data frames, vectors, and statistical models as first-class citizens. Python grew as a general-purpose language, so its data tools (pandas, NumPy) were added on top of a language built for other things. In practice, R tends to win for statistical modeling, academic research, and quick exploratory analysis, while Python wins for production software, deep learning, and general-purpose scripting.
readr
readr provides fast, consistent functions for reading flat files like CSVs into R, such as `read_csv()`, as an improvement over base R's equivalent functions. It handles more edge cases well, like inconsistent column types or unusual encodings, and gives clearer feedback about how it parsed each column. It's typically the first tool reached for when loading tabular data into R.
Recursion
Recursion is when a function calls itself, typically to break a problem into smaller versions of the same problem until it reaches a simple base case. A classic example is calculating a factorial by having the function call itself with a smaller number each time. Recursive functions need a clear stopping condition, or they will call themselves indefinitely and crash.
Regular Expressions
Regular expressions are a pattern-matching language for describing sequences of characters, used to search, validate, or extract specific pieces of text. A pattern like `^\d{3}-\d{4}$` might describe a phone number format, matching only text that fits that exact shape. They take practice to read fluently, but solve text problems that simple string functions alone can't handle efficiently.
Relationship Analysis
Relationship analysis examines how two or more variables relate to each other, using tools like scatterplots for two numeric variables, correlation matrices for many numeric variables at once, and cross-tabulation for categorical variables. This is usually the second step in exploratory analysis, after looking at variables individually. It often surfaces the patterns that later motivate a specific statistical model.
renv
renv manages package versions on a per-project basis instead of one shared library for your whole machine. It records the exact package versions a project uses in a lockfile, so anyone who opens that project later can restore the exact same environment. This solves the common problem where a script that worked months ago breaks because a package updated in a way that changed its behavior.
Reshaping
Reshaping changes the layout of a dataset without changing its underlying content, most commonly converting between wide and long formats. Wide format spreads related values across separate columns, while long format stacks them into fewer columns with an extra column identifying what each value represents. Many statistical functions and plotting tools expect one shape or the other, so reshaping is often a necessary step before analysis.
rstatix
rstatix provides a tidyverse-friendly interface for common statistical tests, wrapping base R functions like `t.test()` and `aov()` in a syntax that fits naturally into a dplyr pipeline. It returns results as tidy data frames rather than the more complex object types base R statistical functions typically produce, making the output easier to filter, combine, or feed into a plot. It's a popular choice for anyone who wants classical statistical tests without leaving the tidyverse workflow.
RStudio
RStudio is the most established integrated development environment for R, combining a script editor, console, plot viewer, and package tools in one window. It has deep, mature support for building R packages and rendering Quarto or R Markdown documents. Posit develops and maintains it, and much existing R documentation still assumes you're working in it.
Sampling
Sampling covers the different methods for selecting a subset of a population to study, when studying the entire population isn't feasible, including simple random, stratified, and cluster sampling. It also covers the mechanics of random number generation in R and setting a seed for reproducibility. The right sampling method depends on the structure of the population and what you need the sample to represent.
Scopes
Scoping determines which variables a function can see and use while it runs. R uses lexical scoping, meaning a function looks first inside itself, then in the environment where it was defined, not the environment where it was called from. Understanding scoping becomes especially important once you write nested functions or functions that return other functions.
Setting Up
This section covers configuring your R environment: managing packages, choosing an IDE, and setting up tools for publishing your work. It's the practical groundwork that makes the rest of your R workflow smooth.
sf
sf handles vector spatial data in R, representing points, lines, and polygons, such as store locations or country boundaries. It's built around simple features, a standard format for representing spatial data used across many GIS tools beyond just R. It's the modern standard for vector geospatial work in R, having replaced the older sp package.
Shiny
Shiny is a framework for building interactive web applications directly in R, without needing to know HTML, CSS, or JavaScript. An app has two main parts: a UI defining what the user sees, and a server function defining how the app responds when the user interacts with those elements. This reactive model automatically updates outputs whenever their underlying inputs change.
Sparklyr
Sparklyr connects R to Apache Spark, a distributed computing framework built for processing datasets far too large to fit in memory on a single machine. It lets you write familiar dplyr syntax that gets translated into Spark operations running across a cluster of machines. It matters specifically once a dataset grows beyond what fits comfortably in a single machine's memory.
Statistical Analysis
This section covers the classical statistical techniques used to summarize data, test hypotheses, and model relationships between variables. It's the theoretical and methodological core underneath a lot of applied data work.
stats
stats is the base R package that ships with every R installation, providing core statistical functions like `lm()` for regression, `t.test()` for hypothesis testing, and functions for common probability distributions. It requires no separate installation since it's part of base R itself. Many higher-level statistics packages build directly on functions from this package.
Chuyên Gia & Mở Rộng Hệ Thống
Giai đoạn 5 tập trung hoàn thiện 25 chủ đề then chốt.
String Manipulation
String manipulation covers searching, extracting, replacing, and combining text values, most commonly using the stringr package's consistent `str_` functions. Text data shows up constantly in real datasets: names, categories, free-text responses. Getting comfortable with these operations resolves a large share of everyday data cleaning problems.
stringr
stringr provides a consistent, well-documented set of functions for working with text, covering tasks like detecting patterns, extracting substrings, and combining strings. Every function follows a consistent naming pattern starting with `str_`, such as `str_detect()` or `str_replace()`, making the package easier to learn than base R's more scattered string functions. It's the standard tool for text-heavy cleaning and manipulation in modern R code.
Strings and Dates
This section covers handling text and date or time data, two of the most common sources of messy, inconsistently formatted values in real datasets. Getting comfortable here pays off across nearly every other part of data analysis.
strip, replace, split
Stripping removes leading or trailing whitespace from text, replacing swaps out unwanted characters or patterns for something else, and splitting breaks a single string into multiple parts based on a delimiter. These are usually among the first steps in cleaning any dataset with free-text or inconsistently formatted columns. The stringr package provides consistent functions for all three operations.
Summary Statistics
Summary statistics condense a column of data down to a few representative numbers, like the mean, median, and standard deviation. They form the foundation for almost every deeper statistical technique, since you need to understand a variable's basic behavior before modeling relationships involving it. Calculating these is usually the first step in getting a feel for any new dataset.
terra
terra handles raster data in R, grids of values like satellite imagery or elevation data, and has replaced the older, now retired raster package. It provides functions for reading, manipulating, and analyzing raster layers, including operations like resampling and calculating values across a grid. It's the standard modern choice for raster work in R.
Text
Plain text files store unstructured or loosely structured data without the strict comma or tab delimiters of a CSV or TSV file. Reading them into R might mean reading line by line for further parsing, or using a delimiter-aware function if the text has some consistent structure like tab separation. This is a common starting point for log files, raw scraped content, or data that doesn't fit neatly into a standard tabular format.
tibble
tibble is the tidyverse package that defines and supports the tibble data structure, a modern variant of the data frame. It provides functions for creating tibbles directly, converting existing data frames into tibbles, and controlling how they print in the console. Most other tidyverse packages return tibbles by default, relying on this package underneath.
Tibbles
A tibble is a modern variant of the data frame, introduced by the tidyverse, with a few behavioral differences designed to reduce surprises. Tibbles print more usefully in the console, showing only what fits on screen along with each column's type, and they refuse to silently drop dimensions the way base data frames sometimes do. Most tidyverse functions return tibbles by default rather than base data frames.
tidygraph
tidygraph applies tidyverse conventions to network data, letting you manipulate graph nodes and edges using familiar dplyr-style verbs instead of igraph's more specialized syntax. It works alongside ggraph for visualization, together forming a tidyverse-native alternative to working with igraph directly. It's often preferred by users already comfortable with the tidyverse who want that same syntax applied to network data.
tidymodels
tidymodels is a collection of R packages for building and evaluating machine learning models, designed to follow tidyverse conventions and work well with the pipe operator. It splits modeling into distinct, composable steps: preprocessing, specifying a model, fitting it, and evaluating results, each handled by a different package within the collection. It has become the standard modern framework for machine learning in R.
tidyr
tidyr handles reshaping data between different layouts, most notably converting between wide and long formats with `pivot_longer()` and `pivot_wider()`. Many statistical functions and ggplot2 specifically expect data in a particular shape, so tidyr often bridges the gap between how data is stored and how it needs to look for analysis or plotting. It also handles filling in missing combinations of values and unnesting nested data structures.
tidytext
tidytext applies tidyverse conventions to text analysis, representing tokenized text as a data frame with one row per word. This tidy structure lets you use familiar dplyr verbs to filter, count, and summarize text data, rather than working with specialized text-only data structures. It's typically the entry point before deeper text analysis like sentiment analysis or topic modeling.
tidyverse
The tidyverse is a collection of R packages designed to work together around a shared philosophy and consistent syntax for data science tasks. It includes packages for data manipulation, visualization, string handling, and more, all built to interoperate smoothly with the pipe operator. Learning the tidyverse's conventions once makes each individual package inside it easier to pick up.
Time Series Analysis
Time series analysis studies data collected sequentially over time, where the order of observations carries meaning that would be lost if the rows were shuffled. It involves identifying trends, seasonal patterns, and autocorrelation, where a value depends on its own previous values. Forecasting models use these identified patterns to predict future values based on historical data.
tmap
tmap creates thematic maps in R, designed specifically for visualizing spatial data like sf objects with a syntax inspired by ggplot2's layered approach. It supports both static maps for reports and interactive maps for exploration, switching between the two with a single setting. It's commonly used as the visualization layer on top of data prepared with sf or terra.
Type Conversion
Type conversion is deliberately changing a value from one type to another, such as turning the character `"5"` into the numeric value `5` with `as.numeric()`. R also does this automatically in some situations, called coercion, which can cause quiet bugs when data doesn't convert the way you expect. Understanding both the explicit functions and R's automatic rules helps catch these issues early.
Vectors
A vector is R's most basic data structure: an ordered collection of values that all share the same type. Nearly everything in R is built on vectors, including single values, which R treats as vectors of length one. Operations on vectors work element by element without needing an explicit loop, which is one of the defining features of how R code is written.
Visual Inspection
Visual inspection uses plots, most often boxplots or scatterplots, to spot outliers by eye rather than through a fixed numeric rule. This often reveals context that purely numeric methods miss, such as whether an unusual value looks like a data entry error or a genuine rare event. It's frequently used alongside numeric methods like IQR or Z-score rather than as a replacement for them.
VS Code
VS Code is a general-purpose code editor from Microsoft that supports R through extensions rather than built-in features. It fits projects where R is just one part of a larger codebase involving other languages, since it shares one consistent editor across all of them. R support in VS Code is solid but less specialized than what RStudio or Positron offer natively.
Web Scraping
Web scraping extracts data directly from a website's HTML when that data isn't offered through a proper API or downloadable file. The rvest package reads a webpage's HTML and lets you select specific elements using CSS selectors or XPath, pulling out text, tables, or links. It requires some understanding of how the target page is structured, since different sites organize their HTML very differently.
Window Functions
Window functions calculate a value for each row based on a set of related rows, without collapsing the data the way summarizing does. Ranking each row within its group, calculating a running total, or comparing a row's value to the previous row are all window function tasks. They're typically combined with grouping, keeping one row per original row rather than one row per group.
Working with Data Tables
Working with data tables means using the data.table package's syntax and conventions for fast, memory-efficient data manipulation on large datasets. It uses a distinctive bracket-based syntax, `dt[i, j, by]`, differing from both base R indexing and dplyr's verb-based approach. It's typically reached for once dataset size makes dplyr noticeably slower.
Your First R Script
A first R script is usually a short file with a few lines of code, like assigning a variable and printing it, run to confirm your R installation actually works. Writing and running this first script is where syntax stops being abstract and starts being something you can see execute. It's also the point where you get comfortable with the basic loop of writing code, running it, and reading the output.
Z-score
The Z-score method flags values a certain number of standard deviations away from the mean, commonly using a threshold like 2 or 3. It works well for roughly normally distributed data, but poorly for data that's heavily skewed, since the mean and standard deviation themselves get distorted by skew. It gives a precise numeric measure of how unusual a given value is relative to the rest of the data.