Skip to main content
Statistics LibreTexts

1.8: Summary of important R code

  • Page ID
    33211
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    To help you learn and use R, there is a section highlighting the most important R code used near the end of each chapter. The bold text will never change but the lighter and/or ALL CAPS text (red in the online or digital version) will need to be customized to your particular application. The sub-bullet for each function will discuss the use of the function and pertinent options or packages required. You can use this as a guide to finding the function names and some hints about options that will help you to get the code to work. You can also revisit the worked examples using each of the functions.

    • FILENAME <- read_csv(“path to csv file/FILENAME.csv”)
      • Can be generated using “Import Dataset” button or by modifying this text.
      • Requires the readr package to be loaded (library(readr)) when using the code directly.
      • Imports a text file saved in the CSV format.
    • DATASETNAME$VARIABLENAME
      • To access a particular variable in a tibble called DATASETNAME, use a $ and then the VARIABLENAME.
    • head(DATASETNAME)
      • Provides a list of the first few rows of the data set for all the variables in it.
    • tail(DATASETNAME)
      • Provides a list of the last few rows of the data set for all the variables in it.
    • mean(DATASETNAME$VARIABLENAME)
      • Calculates the mean of the observations in a variable.
    • sd(DATASETNAME$VARIABLENAME)
      • Calculates the standard deviation of the observations in a variable.
    • favstats(DATASETNAME$VARIABLENAME)
      • Requires the mosaic package to be loaded (library(mosaic)) after installing the package).
      • Provides a suite of numerical summaries of the observations in a variable.
    • hist(DATASETNAME$VARIABLENAME)
      • Makes a histogram.
    • boxplot(DATASETNAME$VARIABLENAME)
      • Makes a boxplot.
    • ggplot(data = DATASETNAME, mapping = aes(VARIABLENAME)) +
      geom_histogram(bins =
      10)

      • Makes a histogram with 10 bins using ggplot, requires the ggplot2 library is installed and loaded.

    1


    This page titled 1.8: Summary of important R code is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?