Cookbooks

Practical recipes for real-world R programming and data analysis tasks.

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.

r mutate dplyr data.table transformation

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.

r iteration apply dplyr purrr

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.

r ggplot2 visualization regression statistics

How to calculate correlation between two columns in R

Learn multiple ways to compute correlation coefficients (Pearson, Spearman, Kendall) between columns in R.

r statistics correlation data-analysis

How to calculate a cumulative sum in R

Learn how to compute cumulative sums in R using base R, dplyr, and data.table.

r cumulative-sum vectors dplyr

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.

r data-frame dplyr data.table transformation

How to calculate a rolling mean in R

Learn multiple ways to calculate rolling means and moving averages for time series data in R.

r time-series rolling-window dplyr zoo

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.

r data-analysis na missing-values

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.

r statistics mode vectors dplyr

How to calculate row-wise statistics in R

Calculate statistics across rows in a data frame using base R, dplyr, and data.table.

r statistics row-wise dplyr

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.

r vectors data-analysis

How to combine two vectors in R

Join vectors together using c() and append() in base R.

r vectors combining base-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.

r percentage dplyr data.table

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.

r statistics summary data-analysis

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().

r data-types lists data-frames tidyverse

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.

r factors data-types tidyverse

How to convert a string to a date in R

Convert character strings to Date objects using base R, lubridate, and readr.

r dates lubridate string-conversion

How to convert wide format to long format in R

Reshape data from wide to long format using tidyr, data.table, and base R.

r reshaping tidyr data.table tidyverse

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.

r sequences vectors base-r

How to count rows by group in R

Count rows by group using dplyr, base R, and data.table with examples.

r count group-by dplyr data.table

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.

r frequency table dplyr data-table

How to create a bar chart with ggplot2 in R

Create bar charts in R using ggplot2 with geom_bar and geom_col.

r ggplot2 visualization bar-chart

How to detect outliers in a vector in R

Learn multiple statistical methods to identify outliers in R using IQR, Z-scores, and boxplots.

r statistics outliers data-cleaning

How to create a column based on conditions in R

Create conditional columns using ifelse(), case_when(), and base R methods for data transformation.

r ifelse case_when dplyr conditional transformation

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.

r ggplot2 visualization scatter-plot graphics

How to Drop a Column from a Data Frame in R

Remove columns from data frames using base R, dplyr, and data.table.

r data-frame dplyr data.table transformation

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.

r factors data-types tidyverse

How to extract coefficients from a model in R

Learn how to extract model coefficients using coef() and broom in R.

r statistics regression models

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.

r unique duplicates dplyr data.table vector

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.

r filtering dplyr 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.

r duplicates dplyr data.table data-frame

How to format a date in R

Convert and format dates in R using base R, lubridate, and strftime patterns.

r dates lubridate formatting

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.

r statistics regression linear-model

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.

r formatting strings display

How to join two data frames in R

Join data frames by common columns using dplyr, base R merge(), and data.table.

r joining dplyr data.table merge

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.

r http api httr2 web

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.

r tidyr pivot data-reshaping tidyverse

How to group and summarise a data frame in R

Group data and compute summary statistics using dplyr, base R, and data.table.

r group-by summarise dplyr

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.

r histogram visualization ggplot2 base

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.

r joining dplyr data.table merge

How to Read an Excel File in R

Learn multiple ways to read Excel files into R using readxl and openxlsx.

r excel data-import readxl openxlsx

How to Read a CSV File in R

Learn multiple ways to read CSV files into R using base R, readr, and data.table.

r csv data-import readr data.table

How to rename columns in a data frame in R

Rename columns in a data frame using base R, dplyr, and data.table.

r dplyr data-table column-rename

How to remove NA values from a vector in R

Filter out missing values using base R, dplyr, and tidyr functions.

r na missing-data dplyr tidyr

How to Repeat a Value N Times in R

Learn how to repeat values multiple times using rep() in base R.

r rep repeat vectors

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.

r na missing-data dplyr data.table

How to round numbers in R

Round numbers using base R functions like round(), floor(), ceiling(), and trunc().

r rounding numbers

How to replace values in a column in R

Replace values in a data frame column using base R, dplyr, and data.table.

r replacement dplyr data.table

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.

r sample random dplyr data.table slice

How to Run a t-Test in R

Perform one-sample, two-sample, and paired t-tests in R using base stats and broom.

r statistics t-test hypothesis-testing inference

How to Sample Without Replacement in R

Learn how to randomly sample elements without replacement using base R and tidyverse functions.

r sample random without-replacement dplyr vector

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.

r ggplot2 visualization export graphics

How to sort a vector in descending order in R

Sort a vector from highest to lowest using base R and data.table.

r sorting vectors

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.

r stringr tidyr string-splitting data-transformation

How to select columns by name in R

Select columns from a data frame by name using base R, dplyr, and data.table.

r selection dplyr data.table

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.

r excel data-export openxlsx writexl

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.

r subsetting filtering dplyr 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.

r csv data-export readr data.table

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.

r csv dplyr data-analysis

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.

r sorting dplyr data.table