Appendix C — Popular functions

C.1 Help

  • ?: Search R documentation for a specific term.
  • ?? Search R help files for a word or phrase.
  • RSiteSearch: Search search.r-project.org
  • help.start: Access to html manuals and documentations implemented in R
  • browseVignettes: view a list of all vignettes associated with your installed packages
  • vignette: View a specified package vignette, that is, supporting material such as introductions.

C.2 Package management

  • install.packages: Installs packages from CRAN.
  • pacman::p_load: Installs and loads specified R packages.
  • library: (Install and) loads specified R packages.

C.3 General

  • setwd: Sets the working directory to the specified path.
  • rm: Removes objects (variables) from the workspace.
  • sessionInfo: Information about the R environment.
  • source: Executes R code from a file.

C.4 Tools

  • else: Execute a block of code if the preceding condition is false.
  • else if: Specify a new condition to test if the first condition is false.
  • if: Execute a block of code if a specified condition is true.
  • ifelse: Check a condition for every element of a vector.

C.5 Data import

  • c: Combine values into a vector or list.
  • read.csv: Reads a CSV file into a data frame.
  • read_dta: Read Stata dataset.
  • load: Loads an RData file.

C.6 Inspect data

  • dim: Returns the dimensions (number of rows and columns) of a data frame.
  • glimpse: Provide a concise summary.
  • head: Returns the first elements.
  • print: Prints the specified object.
  • names: Returns the variable names in a data frame.
  • n() or nrow(): Counts the number of observations in a data frame or group of observations.
  • ncol: Returns the number of columns in a data frame.
  • summary: Summary statistics.
  • table: Create a table of counts or cross-tabulation.
  • tail: Returns the last n elements.
  • unique: Extracts unique elements from a vector.
  • view: Opens a viewer for data frames.

C.7 Graphics

  • abline: Adds lines to a plot.
  • aes: Aesthetic mapping in ggplot.
  • facet_wrap: Creates a grid of facetted plots.
  • geom_hline: Adds horizontal lines to a ggplot.
  • geom_line: Adds lines to a ggplot.
  • geom_point: Adds points to a ggplot.
  • geom_smooth: Adds a smoothed line to a ggplot.
  • geom_text: Adds text to a ggplot.
  • geom_vline: Adds vertical lines to a ggplot.
  • ggsave: Saves a ggplot to a file.
  • labs: Adds or modifies plot labels.
  • plot: Creates a scatter plot.
  • scale_y_reverse: Reverses the y-axis in a ggplot.
  • stat_smooth: Adds a smoothed line to a ggplot.
  • theme_classic: Applies a classic theme to a ggplot.
  • theme_minimal: Applies a minimal theme to a ggplot.

C.8 Data management

  • arrange: Reorder the rows of a data frame.
  • clean_names: Cleans names of an object (usually a data.frame).
  • complete: Completes a data frame with all combinations of specified columns.
  • data.frame: Creates a data frame.
  • distinct: Removes duplicate rows from a data frame.
  • identical: Check if two objects are identical.
  • is(na): Identify and flag a missing or undefined value (NA).
  • is_tibble: Check if an object is a tibble.
  • rm: Removes objects (variables) from the workspace.
  • relocate: Reorders columns in a dataframe.
  • round: Rounds a numeric vector to the nearest integer.
  • rownames: Get or set the row names of a matrix-like object.
  • tibble: Creates a tibble, a modern and tidy data frame.

C.9 dplyr functions

  • arrange: Reorder the rows of a data frame.
  • complete: Completes a data frame with all combinations of specified columns.
  • ends_with: matches to a specified suffix
  • filter: Pick observations by their values.
  • first: Returns the first element.
  • group_by: Group data by one or more variables.
  • last: Returns the last element.
  • mutate: Add new variables or modify existing variables in a data frame.
  • nth: Returns the nth element.
  • n_distinct: Returns the number of distinct elements.
  • rename: Rename variables in a data frame.
  • rename_all: Renames all variables in a data frame.
  • row_number: Adds a column with row numbers.
  • rowwise: Perform operations row by row.
  • select: Pick variables by their names.
  • select_all: Selects all columns in a data frame.
  • slice_head: Selects the top N rows from each group.
  • starts_with: Select variables whose names start with a certain string.
  • summarise: Reduce data to a single summary value.

C.10 Data analysis

  • aggregate: Apply a function to the data by levels of one or more factors.
  • anti_join: Return rows from the first data frame that do not have a match in the second data frame.
  • cor: Computes correlation coefficients.
  • cov: Computes covariance.
  • diff: Calculates differences between consecutive elements.
  • get_dupes: Identify duplicate rows in a data frame (from the janitor package).
  • paste0: Concatenate vectors after converting to character.
  • predict: Predict method for model fits.
  • prop.table: Create a table of proportions.

C.11 Statistical functions

  • cor(): Computes correlation coefficients.
  • cov(): Computes the covariance.
  • exp(): Exponential function.
  • IQR(): Computes the interquartile range.
  • kurtosis(): Computes the kurtosis.
  • log(): Natural logarithm.
  • mad(): Computes the mean absolute deviation.
  • max(): Returns the maximum value.
  • mean(): Calculates the mean.
  • median(): Computes the median.
  • min(): Returns the minimum value.
  • quantile(): Computes sample quantiles.
  • sd(): Calculates the standard deviation.
  • skewness(): Calculates the skewness.
  • var(): Calculates the variance.