Chapter 3 Introduction to R

3.1 Object and Mode

In R, an object is anything that can be assigned to a variable. Mode is the value of the object. Mode can be integer, complex (as in complex number), logical (as in true or false), character (similar idea to categorical variable) and more.

3.2 Data Structures

R has a wide variety of objects for holding data. They include vectors, matrices, arrays, data frames, and more.

  • Vectors are one-dimensional arrays that can hold numeric data, character data or logical data. All elements in each vector have to be of the same mode.
  • Matrices are two-dimensional arrays where each element has the same mode (numeric, character or logical).
  • Arrays are similar to matrices but can have more than two dimensions.
  • Data Frames are similar to matrices, but different columns can contain different modes. It is similar to the idea of datasets.

3.3 Graphics in R

All plots and graphs will appear in the Files/Plot/Package/Help/Viewer or 4th panel. Plots and graphs can be exported as either an image or a pdf file. Check the Directory to make sure your file goes where you want it to go. It is a good idea to set your working directory beforehand.

3.4 GGPlot2 Package

When graphics are involved, you will be shown an alternative method to Basic R called Ggplot2. Ggplot2 has some nice features for data visualization. The package is called ggplot2, but the command function is called ggplot. Before you can use the ggplot function, the package ggplot2 has to be installed and loaded.

Note: When graphics are involved, you have the option of coding in Basic R or Ggplot2, whichever one you prefer. There is no need to know or do both Basic R and Ggplot2.