rguides

Cookbooks

Cookbooks

Practical recipes for real-world R programming tasks.

  1. How to Append Rows to Data Frames in R

    Append rows to data frames using base R rbind() or dplyr bind_rows(). Collect rows in a list first, then bind once for better performance with large data.

  2. How to Add Trend Lines to Scatter Plots in R

    Add trend lines to scatter plots using ggplot2 geom_smooth(). Use method = lm for linear fits or loess for smoothed curves, with optional confidence intervals.

  3. How to Apply Functions Row-Wise to Data Frames in R

    Apply functions to each row of a data frame with base R apply(), dplyr rowwise(), or purrr pmap(). Prefer vectorized operations when possible for better speed.

  4. How to Calculate Correlation Between Columns in R

    Calculate correlation between columns using Pearson, Spearman, or Kendall methods with cor(). Handle missing data and test significance with cor.test().

  5. How to Calculate Cumulative Sums in R with cumsum()

    Calculate cumulative sums on vectors and data frames with cumsum() or dplyr mutate(). Group by categories to reset accumulation per group boundary.

  6. How to Calculate Rolling Means in R

    Calculate rolling means in R with slider and zoo to smooth time series data. Choose centered or trailing windows to spot trends without lookahead bias.

  7. How to Add Calculated Columns to Data Frames in R

    Add calculated columns to data frames with base R or dplyr mutate(). Chain dependent calculations and reference newly created columns in one step.

  8. How to Calculate Statistical Mode in R

    Calculate statistical mode — the most frequent value in a vector — using base R custom functions. Handle ties, NAs, and data frame columns with practical code.

  9. How to Calculate Row-Wise Statistics in R

    Calculate row-wise statistics across data frame columns using rowMeans, apply, and dplyr rowwise. Handle NA values and pick the fastest approach for your data.

  10. How to Check NA Values in R Data Frames

    Check NA values in data frames using is.na, complete.cases, and dplyr. Find missing spots in your data and decide whether to impute or drop incomplete rows.

  11. How to Check Value Membership in R Vectors

    Check value membership in R vectors with %in% and match(). Filter data frames by inclusion, exclude values, and write clean subsetting code.

  12. How to Combine Vectors in R with c() and append()

    Combine vectors with c() and append() in R. Understand type coercion rules when mixing types and insert elements at any position in an existing vector.

  13. How to Compute Row Percentages of Total in R

    Compute row percentages of total in R with dplyr mutate and sum(), grouped calculations, vector division, and na.rm for handling missing values.

  14. How to Compute Summary Statistics for Columns in R

    Compute summary statistics for columns in R with dplyr summarise(), base R summary(), and skimr for exploratory analysis of mean, median, sd, min, and max.

  15. How to Convert Columns to Factors in R

    Convert columns to factors in R using factor() with custom level ordering, as.factor() for quick conversion, and dplyr mutate for ordered factors with labels.

  16. How to Convert Lists to Data Frames in R

    Convert lists to data frames in R with as.data.frame() for equal-length elements, bind_rows for mismatched lists, and rbindlist for large datasets.

  17. How to Convert Strings to Dates in R with lubridate

    Convert strings to dates in R using lubridate ymd(), mdy(), dmy() for automatic format detection, and base R as.Date() with format codes for precise control.

  18. How to Count Rows by Group in R with dplyr

    Count rows by group in R with dplyr count() for frequency summaries and base R table() for simpler cases, plus sorting, multiple groups, and weighted counts.

  19. How to Create Sequences of Numbers in R

    Create sequences of numbers in R with seq() for step and length arguments, seq_along() for safe index generation, and rep() for repeating values in patterns.

  20. How to Convert Wide Data to Long Format in R

    Convert wide data to long format in R with tidyr pivot_longer() for tidy workflows, data.table melt() for large datasets, and multiple value columns.

  21. How to Create Conditional Columns in R with dplyr

    Create conditional columns in R with dplyr case_when() for multiple conditions and if_else() for single true/false splits, using vectorised row evaluation.

  22. How to Create Frequency Tables in R with table() and dplyr

    Create frequency tables in R with base R table() and dplyr count() for proportional conversion, NA handling, and two-way cross-tabulations in tidy data frames.

  23. How to Create Bar Charts with ggplot2 in R

    Create bar charts with ggplot2 in R using geom_bar() for automatic counts and geom_col() for pre-summarised values, with fill colours, stacking, and dodging.

  24. How to Create Scatter Plots with ggplot2 in R

    Create scatter plots with ggplot2 in R using geom_point() with colour-by-group mapping, size aesthetics, trend lines, and faceting for multi-panel layouts.

  25. How to Detect Outliers in a Vector in R

    Detect outliers in R using the IQR method and boxplot.stats(). Finding outliers is a key data cleaning step that belongs before any statistical analysis.

  26. How to Drop Column from Data Frame in R

    Drop column from data frame using dplyr select(), base R NULL assignment, or data.table's := operator. Three ways to drop column in R for different pipelines.

  27. How to Encode Factor Variable as Numeric in R

    Encode factor variable as numeric in R using as.numeric() for integer codes and as.numeric(as.character()) to recover original numbers.

  28. How to Extract Unique Values from a Vector in R

    Extract unique values in R using unique() for vectors, distinct() for data frames, and n_distinct() to count them. Remove duplicates in both base R and dplyr.

  29. How to Filter Rows by a Condition in R

    Filter rows by condition in R using dplyr::filter(). Combine conditions with & and |, handle NA values properly, and build readable transformation pipelines.

  30. How to Extract Coefficients from a Model in R

    Extract coefficients from a fitted model in R using coef() for a named vector or broom::tidy() for a data frame with estimates, standard errors, and p-values.

  31. How to Find Duplicate Rows in R and Remove Them

    Find duplicate rows in R using duplicated() and dplyr::distinct(), then remove them in one pipeline. Covers base R, dplyr, and data.table approaches.

  32. How to Format Dates in R with format() and lubridate

    Format dates in R with format() strptime codes and lubridate helpers like stamp(). Convert Date objects to readable strings in ISO, US, and European formats.

  33. How to Fit Linear Model and Interpret Results in R

    Fit linear model in R with lm() and interpret coefficients, R-squared, fitted values, and diagnostic plots. Covers simple and multiple regression.

  34. How to Format Numbers with Commas in R Using format() and scales

    Format numbers with commas in R using format() big.mark and scales::comma(). Add thousand separators to console output, tables, and ggplot2 axis labels.

  35. How to Group Data and Summarise with dplyr in R

    Group data in R with dplyr group_by() and summarise(), base R aggregate(), and data.table. Compute mean, count, and multiple stats per group in one pipeline.

  36. How to Join Two Data Frames in R with dplyr and merge()

    Join two data frames in R by common columns with dplyr left_join(), base R merge(), and data.table. Covers left, inner, right, and full joins with examples.

  37. How to Make HTTP Requests in R with httr2

    Make HTTP requests in R with httr2 — a pipe-friendly package covering GET, POST, authentication, JSON parsing, rate limiting, and error handling.

  38. How to Pivot Data from Wide to Long Format in R

    Learn how to pivot data from wide to long format in R using tidyr::pivot_longer(), with examples for basic reshaping and selecting columns.

  39. How to Merge Data Frames with Different Key Names in R

    Learn how to merge data frames with different key names in R using dplyr left_join and base R merge, with examples for mismatched join columns.

  40. How to Plot Histograms in R Using ggplot2 and Base R

    Plot histograms in R with ggplot2 geom_histogram() and base R hist(), covering bin width control, colors, density overlays, and grouped panels.

  41. How to Read CSV Files in R with readr and Base R

    Read CSV files in R with readr read_csv() and base R read.csv(), covering column types, delimiter handling, and fast alternatives for large datasets.

  42. How to Remove NA Values from Vectors in R

    Remove NA values from vectors and data frames using base R, dplyr, and tidyr. NA values represent missing data in R and can bias your analysis.

  43. How to Rename Columns in Data Frames in R

    Rename columns in a data frame using dplyr, base R, and data.table. A frequent data cleaning task that makes datasets easier to work with.

  44. How to Read Excel Files in R with readxl

    Read Excel files in R using readxl::read_excel(), covering sheet selection, column type specification, and common import patterns for .xls and .xlsx workbooks.

  45. How to Repeat Values in R with rep()

    Repeat values in R using rep() to create vectors by cycling through elements, repeating each element in place, or filling to a fixed length.

  46. How to Replace NA Values with Specific Values in R

    Replace NA values in vectors and data frames using base R, dplyr, and data.table. A common data cleaning step that replaces missing data with a chosen default.

  47. How to Replace Values in Data Frame Columns in R

    Replace values in a data frame column using base R, dplyr, and data.table. Covers conditional replacement, bulk find-and-replace, and NA handling.

  48. How to Run t-Tests in R with t.test()

    Run t-tests in R with t.test() for one-sample, two-sample, and paired comparisons. Returns p-values and confidence intervals in a single call.

  49. How to Round Numbers in R

    Round numbers using base R functions like round(), floor(), ceiling(), and trunc(). R uses banker's rounding where halves round to the nearest even digit.

  50. How to Sample Random Rows from Data Frames in R

    Sample random rows from data frames with dplyr slice_sample(), base R sample(), and data.table. For train/test splits, bootstrap, and randomising data.

  51. How to Sample Without Replacement in R with sample()

    Sample without replacement from a population using sample() and dplyr. Each element appears once at most, ideal for subsets and random assignments.

  52. How to Save ggplot Charts to Files in R with ggsave()

    Save ggplot charts to PNG, PDF, and SVG files with ggsave(). Control resolution, dimensions, and background colour for publication-quality output in R.

  53. How to Select Columns by Name in R with dplyr::select()

    Select columns from data frames by name with dplyr select(), base R, and data.table. Use helpers like starts_with() and where() for flexible extraction.

  54. How to Sort Data Frames by Column in R with arrange()

    Sort data frames by column with dplyr arrange(), base R order(), and data.table setorder(). Stable sorts keep tied rows in their original relative order.

  55. How to Sort Vectors in Descending Order in R

    Sort vectors from highest to lowest with base R sort(), order(), and data.table. Use decreasing = TRUE for descending order in a single function call.

  56. How to Split String Columns into Multiple Columns in R

    Split string columns into multiple fields with strsplit(), stringr str_split(), and tidyr separate(). A common data cleaning task in R.

  57. How to Subset Data Frames by Multiple Conditions in R

    Subset data frames by several criteria with AND, OR, and NOT operators. Works across base R, dplyr, and data.table with only minor syntactic differences.

  58. How to Write Data Frames to CSV Files in R

    Write data frames to CSV with readr write_csv(), base R write.csv(), and data.table fwrite(). A routine export task in R data analysis pipelines.

  59. How to Read CSV Files and Summarise with dplyr in R

    Read CSV files and produce grouped summaries with readr and dplyr group_by() and summarise(). A fast pattern for initial data exploration in R.

  60. How to Write Data Frames to Excel Files in R

    Write data frames to Excel with openxlsx for formatting and writexl for quick exports. Handles multi-sheet and single-sheet xlsx files with R.