Installation

Please watch the tutorial videos below on how to install R and RStudio.

So, R is a free software for statistical computing and graphics. But, what is RStudio and why is it recommended? Your answers in the end of this semester will probably include some of the reasons given in this article, Top 6 Reasons You Need to be Using RStudio.

R Interface

RStudio Interface

The largest subpanel on the left is the R console. Yes, R console is embeded in RStudio.

The buttons on the top right corner of each subpanel allow us to minimize, maximize, or resize the particular subpanel. We will learn about the other subpanels as we move along.

R 101

We can either use the R console in the R interface or in the RStudio interface. For now, the choice does not make any difference. But, when task gets more complicated, the advantage of using RStudio will be clear.

  1. Basic calculations. Try the following commands in an R console.
3 * 7 
15 / 5
9 ^ 2
sqrt(36)

Practice Calculate \(0.43 + 1.96 \times \sqrt{\frac{0.43(1 - 0.43)}{108}}\).

  1. Input data manually. For example, the ages of five people are 34, 82, 71, 36, 68.
age <- c(34, 82, 71, 36, 68)

We deposited the data in the variable age. This was done in two steps.

  • The c function combined its arguments (i.e., the ages) and creates a list.
  • The arrow sign (<-) deposited the content on the right-hand side of the arrow (i.e., the ages) to the R variable on the left-hand side of the arrow (i.e., the variable age).

Now if we type age in the console, then its content is printed.

age
  1. Read data from a file.

Of course, it is not practical to input larger dataset manually. Instead, we can read a large dataset from a file. For example

df <- read.csv("https://albums.yuanting.lu/sta126/data/credit.csv")

The line reads the dataset from the file credit.csv and saves it in the R variable df, so that from now on, whenever we type df, R pulls out this saved dataset. Let’s try it.

df