Introduction to R#

R is a programming language and environment specifically designed for statistical analysis and graphics. It is widely used among researchers, statisticians and data miners for data analysis and developing statistical software.

To start using R, the first step is to install it. R is available for free on Windows, Mac, and Linux operating systems, and you can download it from the Comprehensive R Archive Network (CRAN) website. For Windows and Mac users, it’s recommended to download and install the pre-compiled binary versions.

For detailed instructions on downloading and installing R and RStudio, you can watch this step-by-step video guide.

Installing R and RStudio#

For Windows Users To install R on Windows:

  1. Go to the CRAN website and select the ‘Download R for Windows’ link.

  2. Click on the ‘base’ link.

  3. Download the file by clicking on the ‘Download R 4.3.3 for Windows’ link.

Once the download is complete, double-click the .exe installation file and follow the on-screen instructions. Detailed installation instructions are available on the CRAN website.

For Mac Users To install R on a Mac:

  1. Go to the CRAN website and select the ‘Download R for (Mac) OS X’ link.

  2. Download the binary by clicking on the ‘R-4.3.3.pkg’ link.

After the download, double-click on the file icon and follow the on-screen instructions to complete the installation. For more details, refer to the ‘R for Mac OS X FAQ’ on the CRAN website.

Installing and Loading Packages#

The base installation of R comes equipped with a wide range of useful packages, many of which you’ll rely on daily. However, as you delve into more diverse projects and your proficiency with R grows, you may find the need to extend R’s capabilities. Fortunately, the R community has developed and shared a vast amount of useful code in the form of installable packages. A package in R is essentially a collection of functions, data, and help files organized in a standardized structure. You can easily download and install these packages to enhance your R environment. The most popular sources for R packages include CRAN, Bioconductor, and GitHub: CRAN: The Comprehensive R Archive Network (CRAN) is the official repository for user-contributed R packages, hosting over 15,000 packages. Bioconductor: This platform specializes in open-source software for bioinformatics and offers over 1,800 R packages. GitHub: A widely-used platform for hosting git repositories, GitHub is not limited to R but often hosts the cutting-edge development versions of R packages. While these versions may include the latest features, they might not be as stable as the versions available on CRAN, as they are still under development. Additionally, updates to packages from GitHub are not automatic. By exploring these resources, you can significantly expand the functionality of your R environment to meet your specific needs.

Installing Packages: The install.packages(“ggplot2”) command installs the ggplot2 package from CRAN (The Comprehensive R Archive Network). Loading Packages: The library(ggplot2) command loads the ggplot2 package into your R session so you can use its functions.

Working Directories#

The working directory in R is the default location where R looks for files to load and where it saves any files you create. One of the advantages of using RStudio Projects is that when you open a project, RStudio automatically sets the working directory to the correct location for that project. To check the file path of your current working directory, simply glance at the bar at the top of the Console pane.