Introduction

R Software is an integrated suite of open-source software facilities for data manipulation, calculation and graphical display. RStudio is an integrated development environment (IDE) that allows us to interact with R more easily. Below is an example of an RStudio session.

Example RStudio Session

RStudio has a user-friendly interface with 4 main windows (panes) to interact with data, including:

  • Console
  • Source/Script
  • Workspace(Global Environment)/History
  • Plots/Files/Packages/Help

Commands

R will echo back what you run in the script (or in the console) in the console. Spacing does not matter, although including spaces is preferred.

2 + 5 # or 2+5
## [1] 7

We call the lines that we run in R commands or code lines. Commands can be separated using a line break or a semicolon (;), although line breaks are preferred.

2 * 10
## [1] 20
5 / 2
## [1] 2.5
# or 2 * 10; 5 / 2;

Objects & Variables

Objects/named variables are created in RStudio using an assignment operator. The assignment operators in R are:

  • <- (preferred)
  • =

To assign the number 4 to a variable named y, we would use:

y <- 4

To output value of y, we can run:

y
## [1] 4
  • Objects/variables in R can be:
    • character ("Yes", "No")
    • numeric (1, 1.3, 2.456)
    • logical (TRUE, FALSE)
    • factor (categorical)
is.numeric(y)
## [1] TRUE
is.character("YES")
## [1] TRUE
is.logical(TRUE)
## [1] TRUE

Factor variables are categorical variables, which have class levels, or unique values that they can take on.

All variables created in an RStudio session will be in the workspace/global environment for that session. We can use the ls() function to view the names of the objects in our workspace and can view the objects on the ‘Environment’ tab in RStudio.

ls()
## [1] "y"

Comments

Commenting your code is an important part of the programming process. Comments follow #. You should use comments to identify what your code is doing and why. Comments will display as output in the console but are not evaluated (run).

Basic Operations

RStudio can be used as a (fancy) calculator using both numbers and named variables (objects).

Addition (+)

1 + y
## [1] 5
7 + 10
## [1] 17

Subtraction (-)

10 - 1
## [1] 9
10 - y
## [1] 6

Multiplication (*)

2 * 3
## [1] 6
y * 2
## [1] 8

Division (/)

3 / 2
## [1] 1.5
16 / y
## [1] 4

Exponent (** or ^)

3 ^ y
## [1] 81
2 ** 2
## [1] 4

Relational Operators

We can compare values and/or objects using relational operators. The output will be a boolean value (TRUE/FALSE)

3 > 2 # greater than
## [1] TRUE
4 >= y # greater than or equal to
## [1] TRUE
y < 2 # less than
## [1] FALSE
3 <= 6 # less than or equal to
## [1] TRUE
y == 4 # equal to
## [1] TRUE
y != 6 # not equal to
## [1] TRUE

Functions

We can use built-in functions in R (or functions from packages that we install and load) such as the log() function. Functions can have required and/or optional arguments.

We can use the args() function to view the arguments of the log() function. The args() function has one argument, name, which is used to identify the name of the function that you would like to obtain the arguments for. Any arguments with default values will be shown. Arguments that do not have defaults require user input.

args(name = log)
## function (x, base = exp(1)) 
## NULL

As shown, x is a required argument and base is an optional argument, which defaults to base = exp(1), meaning that the log() function provides the natural log (ln) by default.

log(x = 12) # natural log
## [1] 2.484907

Alternative common bases, such as 2 or 10, can be specified using the base argument.

log(x = 12, base = 10) # log with base 10
## [1] 1.079181

When using functions, if you provide the arguments in the expected order, you do not need to provide the name of the argument.

log(12, 10)
## [1] 1.079181

Some other useful functions include:

abs(): obtain the absolute value of a number (or variable)

abs(x = -230)
## [1] 230

sqrt(): obtain the square root of a number (or variable)

sqrt(x = 4)
## [1] 2

exp(): exponential function (e^n)

exp(x = 3)
## [1] 20.08554

round(): round a value (or variable) to a specified number of digits (default digits = 0)

round(x = 2.356, digits = 2)
## [1] 2.36

Help!

To obtain help or to access documentation for a function in R, we can use ? or the help() function. The help information requested will display in the ‘Help’ panel in RStudio.

?log
help(log)