Cookbooks
Practical recipes for real-world R programming and data analysis tasks.
How to calculate a rolling mean in R
Learn multiple ways to calculate rolling means and moving averages for time series data in R.
How to add a row to a data frame in R
Append new rows to a data frame using base R, dplyr, and data.table.
How to calculate row-wise statistics in R
Calculate statistics across rows in a data frame using base R, dplyr, and data.table.
How to add a trend line to a scatter plot in R
Add linear, polynomial, and smoothed trend lines to scatter plots using ggplot2 and base R.
How to apply a function to each row in R
Learn multiple methods to apply a function to each row of a data frame in R, including base R, dplyr, and purrr approaches.
How to Check for NA Values in a Data Frame in R
Learn multiple ways to detect and count NA values in R data frames using base R and dplyr.
How to add a calculated column to a data frame in R
Add new columns derived from existing data using base R, dplyr, and data.table.
How to calculate a cumulative sum in R
Learn how to compute cumulative sums in R using base R, dplyr, and data.table.
How to calculate the mode in R
Learn how to find the most frequent value (mode) in a vector or data frame column using base R, dplyr, and data.table.
How to calculate correlation between two columns in R
Learn multiple ways to compute correlation coefficients (Pearson, Spearman, Kendall) between columns in R.
How to compute each row as a percentage of total in R
Calculate row percentages in R using dplyr, base R, and data.table.
How to combine two vectors in R
Join vectors together using c() and append() in base R.
How to Check if a Value Exists in a Vector in R
Learn how to check whether a value exists in a vector using %in%, match(), and related functions.
How to compute summary statistics for a column in R
Learn multiple ways to compute summary statistics like mean, median, and standard deviation for data frame columns in R.
How to convert a column to a factor in R
Convert character or numeric columns to factors using factor(), as.factor(), and dplyr::mutate(). Includes handling ordered factors and levels.
How to convert wide format to long format in R
Reshape data from wide to long format using tidyr, data.table, and base R.
How to convert a list to a data frame in R
Convert lists to data frames using as.data.frame(), dplyr::bind_rows(), and data.table::rbindlist().
How to convert a string to a date in R
Convert character strings to Date objects using base R, lubridate, and readr.
How to count rows by group in R
Count rows by group using dplyr, base R, and data.table with examples.
How to create a sequence of numbers in R
Create numeric sequences using seq(), seq_len(), seq_along(), and rep(). Includes step, length, and along-with variants.
How to create a bar chart with ggplot2 in R
Create bar charts in R using ggplot2 with geom_bar and geom_col.
How to Create a Frequency Table in R
Learn how to create frequency tables in R using base R, dplyr, and data.table to count occurrences of values.
How to create a column based on conditions in R
Create conditional columns using ifelse(), case_when(), and base R methods for data transformation.
How to detect outliers in a vector in R
Learn multiple statistical methods to identify outliers in R using IQR, Z-scores, and boxplots.
How to create a scatter plot with ggplot2 in R
Learn how to create and customize scatter plots using ggplot2 in R with the geom_point() function.
How to Drop a Column from a Data Frame in R
Remove columns from data frames using base R, dplyr, and data.table.
How to encode a factor variable as numeric in R
Convert factor variables to numeric values using as.numeric(), level extraction, and tidyverse methods. Handle both regular and ordered factors.
How to extract coefficients from a model in R
Learn how to extract model coefficients using coef() and broom in R.
How to extract unique values from a vector in R
Get distinct values from vectors and data frames using base R, dplyr, and data.table.
How to filter rows by a condition in R
Filter rows from a data frame based on conditions using base R, dplyr, and data.table.
How to find rows with duplicate values in R
Identify, extract, count, and remove duplicate rows in data frames using base R, dplyr, and data.table.
How to fit a linear model with lm() in R
Learn how to fit, interpret, and diagnose linear regression models in R using the lm() function.
How to format numbers with commas in R
Learn multiple ways to add thousand separators (commas) to numbers in R using base R, dplyr, and formatting packages.
How to format a date in R
Convert and format dates in R using base R, lubridate, and strftime patterns.
How to group and summarise a data frame in R
Group data and compute summary statistics using dplyr, base R, and data.table.
How to join two data frames in R
Join data frames by common columns using dplyr, base R merge(), and data.table.
How to merge data frames with different key names in R
Join data frames when column names do not match using dplyr, base R, and data.table.
How to pivot data from wide to long format in R
Transform wide-format data into long format using tidyr pivot_longer(), with examples for various scenarios.
How to Make an HTTP Request in R with httr2
Learn how to make HTTP requests in R using the httr2 package for API interaction.
How to Plot a Histogram in R
Learn how to create histograms in R using base R, ggplot2, and graphics package with detailed examples and customizations.
How to Read a CSV File in R
Learn multiple ways to read CSV files into R using base R, readr, and data.table.
How to remove NA values from a vector in R
Filter out missing values using base R, dplyr, and tidyr functions.
How to Read an Excel File in R
Learn multiple ways to read Excel files into R using readxl and openxlsx.
How to rename columns in a data frame in R
Rename columns in a data frame using base R, dplyr, and data.table.
How to replace NA values with a specific value in R
Replace missing values in vectors and data frames using base R, dplyr, and data.table.
How to Repeat a Value N Times in R
Learn how to repeat values multiple times using rep() in base R.
How to replace values in a column in R
Replace values in a data frame column using base R, dplyr, and data.table.
How to round numbers in R
Round numbers using base R functions like round(), floor(), ceiling(), and trunc().
How to Run a t-Test in R
Perform one-sample, two-sample, and paired t-tests in R using base stats and broom.
How to sample random rows from a data frame in R
Sample random rows from a data frame using base R, dplyr, and data.table with examples.
How to select columns by name in R
Select columns from a data frame by name using base R, dplyr, and data.table.
How to save a ggplot chart to a file in R
Export ggplot2 visualizations to PNG, PDF, SVG, and other formats with proper resolution and dimensions.
How to sort a data frame by a column in R
Sort rows by one or more columns using base R, dplyr, and data.table.
How to sort a vector in descending order in R
Sort a vector from highest to lowest using base R and data.table.
How to split a string column into multiple columns in R
Split character vectors and data frame columns using base R, stringr, and tidyr with practical examples.
How to subset a data frame by multiple conditions in R
Subset rows from a data frame using multiple conditions with AND, OR, and NOT operators in base R, dplyr, and data.table.
How to Write a Data Frame to CSV in R
Learn multiple ways to write data frames to CSV files using base R, readr, and data.table.
How to Sample Without Replacement in R
Learn how to randomly sample elements without replacement using base R and tidyverse functions.
How to Write a Data Frame to an Excel File in R
Learn multiple ways to export data frames to Excel files using openxlsx, writexl, and xlsx packages.
How to read a CSV and summarise it with dplyr
Load a CSV in R with readr and produce a quick grouped summary with dplyr.