An Introduction to R and R Studio

Introduction to R and R Studio

This is the first lecture in the series of lectures on how to use SEMinR package in R for PLS-SEM.

Whatr is R?

  • R is statistical computing language (R Core Team, 2021), which is the software language used to import and clean data as well as create and analyze PLS path models.
  • R is a free, open-source software, which enables users to write and execute code that analyzes data. Readers should note that the name “R” can refer to both the programming language and the primary software that runs code written in this language.
  • Further, open source refers to the kind of software whose underlying code is made freely available and is generally open to suggested improvements or new features built by others.
  • The open-source nature of the R software makes code written in the R language highly reproducible, shareable, testable, scalable, and deployable to larger automated applications.
  • An ever-expanding community of R users supports, tests, documents, and provides add-on resources for each other.
  • The R language was designed with computational statistics in mind. In its simplest form, it can be run from your operating system’s command line or from the R console see (Fig).
  • However, I recommend using R from the convenience of an integrated development environment (IDE), such as RStudio.
  • An IDE is a programming environment that offers tools such as project management, tabs for easily managing multiple script files, and additional developer tools.
  • We discuss the layout of the RStudio IDE in more detail in the next section. Throughout this book, we will demonstrate the use of R from within the RStudio IDE.

Downloading R and R Studio

R Studio Layout

R Studio Layout

R Packages

  • R includes a lot of preinstalled packages containing many of the standard functions and algorithms you will use in your statistical computations.
  • Examples of such standard functions are mean() and sd() for calculating the mean and standard deviation, respectively, or lm() for generating linear regression models.
  • While you should be able to fulfill much of your computational needs with the standard packages bundled in R, you might need to install further software libraries containing newer or more complicated algorithms.
  • Such software libraries are bundled as packages that, when installed, add a new range of functions and operations. Examples of popular packages are dplyr, ggplot, and, of course, the package used in this series, seminr.

Installing R Packages

  • The packages can be installed from the command line or from the packages tab. Note that you will need internet access to install packages from CRAN.
  • To install new packages, select the Packages tab in the lower right window of the RStudio IDE, click the Install button, set Install from to Repository (CRAN), and enter the package name in the Packages field: “seminr”. Next, click on Install.
  • Packages can also be installed from the command line using the install. packages() function. In this case, we wish to install the swirl package, which teaches you R programming (for more details on the swirl package). We therefore set the pkgs parameter equal to “swirl”.

Additionally, you can also install a package from the menu.From Tools Select Install Packages

Loading R Package

  • Note that packages are installed to the local software library on your computer but are not loaded into the RStudio local environment.
  • Once a package is installed, it will be available for computation in R but has to be loaded using the library()function prior to use.
  • Packages must be loaded in each session if you wish to use the functions in this library.
  • If the package is not loaded in a new session (i.e., after opening and rerunning R), the features will not be available in your session until you load the package by using the library() function.
#Load the Swirl Package

Explaining the Syntax

  • Throughout these sessions in the presentation and on tutorials on the website, it will be necessary to discuss various elements of the code when explaining how to perform analytic operations using R.
  • Code will be presented separately throughout the series.

Table provides a summary of the syntax.