Basics to R

Author

P K Parida

Why we are discussing R

- Why R and how it is useful for analysis and report preparation

Typical science project

From: Grolemund and Wickham

Why R

- Its free! If you are a teacher, a student or a user, the benefits are obvious

- It runs on a variety of platforms including Windows, Unix and MacOS

- It provides an unparalleled platform for programming new statistical methods in an easy and straightforward manner

- It offers powerful tools for data exploration and presentation

- It has ample resources on the web

- R is open-source

  • Other statistical software packages can be extremely expensive

  • Large user base with almost all statistical methods implemented

- Import

  • Import data stored in a file, database, or web API, and load it into R

- Tidy

  • Each column is a variable, and each row is an observation

Tidy datasets are all alike but every messy dataset is messy in its own way (Hadley Wickham)

RStudio

A typical RStudio window may look something like this:

  • Script editor: This is where you write the code for your analysis. These could include:

R Script : An R script is basically a series of stored R commands that can be run in the console :- To generate a new script, do : New file -> R Script (ctrl-shift-N)

Rmarkdown document: A document with (or without) R-code.

  • Console: This is where R prints the output of your code when it’s run. You can also write code directly in the console after the > symbol, but that code is not preserved.

  • Environment/History: Have at minimum the following tabs:

    • Environment: Displays all your obtjects (data) and user-defined functions in your current session. One can click on the objects to get a detailed view.

    • History: Contains a list of previous commands entered into the console

    • Additional tabs may include:

      • Connection: Any database connections

      • Build: If your project is webpage/blog project

      • Git: If your project has version control

      • Tutorial: Some nice basic tutorials on R

  • Files/Packages/Help/Viewer:

    • Files - The list of all files contained in your current *working directory*. You can also:
    -   Navigate to different folders on your computer
    -   Create new blank files or folders (directories) 
    -   Delete files or folders 
    -   Rename files or folders 
    -   ... More
    • Plots - Graphical output from R. The user can export the figures to file (as jpeg, png or pdf) or to clipboard

    • Packages - The list of packages (groups of functions) currently installed on your computer. You can install new packages or update existing packages from this tab by clicking Install or Update.

    • Help - Search R documentation to get information what each function does and how. Using the help should be your first stop when trying to figure out how to use a particular function. The lingo may be intitially unfamiliar but with frequent use it becomes more bearable.

    • Viewer: For interactive graphics

Installing R

Latest version of R: see [The Comprehensive R Archive Network). Follow the instructions to complete the installation process:

If your platform is Windows, it is recommended that you install Rtools

RStudio

What is RStudio?

- RStudio is an integrated development environment (IDE)

- It is open-source and free.

- Built to help you write R code, run R code, and analyze data with R

- Text editor, project handling, markdown support, keyboard shortcuts, debugging tools, version control, …

- Within RStudio one can achieve almost all that is needed to complete a typical science project, be it:

  • A technical report

  • A scientific manuscript

  • Web pages (including blogs)

Installing RStudio

Latest version of RStudio Desktop can be obtained from this link

Readings

Two books we recommend to start with

R for Data Science (2e)

ggplot2: Elegant Graphics for Data Analysis (3e)