1.5 Summary of important R code

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