# Chapter 2 Basic R

## 2.1 R as a Calculator

We can compute mathematical expressions by typing them into the console:

7 - 2*(2/5 + 3^2)
[1] -11.8

R follows the basic order of operations: Parenthesis, Exponents, Multiplication, Division, Addition and Subtraction. For instance,

# compare this
9/2 + 3
[1] 7.5
# to this
9/(2 + 3)
[1] 1.8

## 2.2 Variable Assignments

Let us save the result of $$7 - 2*(2/5 + 3^2)$$ to a variable

x <- 7 - 2*(2/5 + 3^2)
x
## [1] -11.8

Here we defined a variable, x, and assigned the computed value to it.

The operator <- is called the assignment operator in R.2

Variable names can contain any combination of alphanumeric characters along with periods (.) and underscores (_). However, they cannot start with a number or an underscore.

It is generally considered best practice to use actual names, usually nouns, for variables instead of single letters. This provides more information to the person reading the code.

Variable names are case sensitive. For instance, weight and Weight are two different variables.

For various reasons a variable may need to be removed. This is easily done using remove or its shortcut rm:

a <- sqrt(37)
b <- a^2 + 5*a
a
## [1] 6.082763

b
## [1] 67.41381

remove(a,b)  # or rm(a,b)

As the example shows remove function accepts multiple arguments separated by commas.

Comments are preceded by the # symbol. Any text appearing after the # is ignored by the R interpreter.

## 2.3 Objects

Everything we encounter in R is considered as an object. They may contain numbers, functions, names, dimensions, etc.

General Structure:(object name) <- (information in R)

object1 <- 1 + 2
object1
## [1] 3

# another object
object2 <- "blue"
object2
## [1] "blue"

## 2.4 Built-in Functions and Operators

Some mathematical operators and functions:

Function R Command Example
Addition + 2+3=5
Subtraction - 2-3=-1
Multiplication * 2*3=6
Division / 2/3=6
Exponent ^ 2^3=8
Square Root sqrt() sqrt(9)=3
Modulus %% 7%%3=1
Absolute Value abs() abs(-2)=2
Transposition t() t(A)
Matrix multip. %*% A%*%B
Inversion solve() solve(A)

To see the help page for functions, for example for sqrt, use either help(sqrt) or enter ?sqrt. However, in the case of an operator like the colon used below, you must enclose the symbol in backticks like this: ?:.

These operators are vectorized, so they will apply to either single numbers or vectors with more than one number.

x <- 2:4
x
## [1] 2 3 4

sqrt(x)
## [1] 1.414214 1.732051 2.000000

The functions seq() and rep() are other ways of generating sequences:

Command Output
seq(3,10) 3, 4, 5, 6, 7, 8, 9, 10
seq(3,10, by = 1.5) 3, 4.5, 6, 7.5, 9
seq(3,10, length = 4) 3, 5.3333333, 7.6666667, 10
seq(along.with = seq(2.5,3.5, by = 0.5)) 1, 2, 3
seq_along(seq(2.5,3.5, by = 0.5)) 1, 2, 3
rep(3, times = 3) 3, 3, 3
rep(c(-1, pi), times = 3) -1, 3.1415927, -1, 3.1415927, -1, 3.1415927
rep(c(-1, pi), each = 3) -1, -1, -1, 3.1415927, 3.1415927, 3.1415927

## 2.5 Workspace and Files Management

The workspace is your current R working environment and includes any user-defined objects (vectors, matrices, functions, data frames, and lists).

The current working directory is the directory from which R will read files and to which it will save results by default. You can find out what the current working directory is by using the getwd() function and you can set the current working directory by using the setwd() function.

It is important to know how to examine your local workspace in R. Here is a list of useful commands for workspace and files management:

Command Description
getwd() shows current working directory
setwd() sets current working directory
ls() lists all the objects in your local workspace
dir() lists all the objects in your current directory
list.files() lists all the objects in your current directory
arg() shows arguments that a function can take
dir.create() creates a directory in the current working directory
file.create() creates a file in your working directory
file.exists() checks if a file exists in your working directory
file.info() shows information about a file
file.rename() changes the name of a file
file.remove() changes the name of a file
file.copy() makes a copy of a file
Command Description
help(options) provides information about available options
options() lets you view or set out current options
history(30) displays last 30 commands (default = 25)
savehistory("my_hist") saves the commands history to my_hist (default = .Rhistory)
loadhistory("my_hist") reloads the commands history
save.image("my_image") saves the workspace to my_image (default = .RData)
save(my_obj, file = "im") saves objects in my_obj to im
load("my_image") loads a workspace into the current session
# save original options
original_options <- options()
# change after decimal digits to 2
options(digits = 2)
# try out
1/3
## [1] 0.33
#set the options to the original back
options(original_options)
1/3
## [1] 0.3333333

The setwd() function won’t create a directory that doesn’t exist. If necessary, you can use the dir.create() function to create a directory and then use setwd() to change to its location.

It’s a good idea to keep your projects in separate directories. You may want to start an R session by issuing the setwd() command with the appropriate path to a project, followed by the load(".RData") command. This lets you start up where you left off in your last session and keeps both your objects and history separate between projects. On Windows and Mac OS X platforms, it’s even easier. Just navigate to the project directory and double-click the saved image file. Doing so starts R, loads the saved workspace, and sets the current working directory to this location.

## 2.6 Input and Output

The source("filename") function submits a script to the current session. If the filename doesn’t include a path, the file is assumed to be in the current working directory. For example, source("myscript.R") runs a set of R statements contained in the file myscript.R. By convention, script filenames end with an .R extension, but this isn’t required.

The sink("filename") function redirects output to the file filename. By default, if the file already exists, its contents are overwritten. Include the option append=TRUE to append text to the file rather than overwriting it. Including the option split=TRUE will send output to both the screen and the output file. Issuing the command sink() without options will return output to the screen alone.

Although sink() redirects text output, it has no effect on graphic output. To redirect graphic output, use one of the functions listed in table below. Use dev.off() to return output to the terminal.

Command Output
bmp("filename.bmp") BMP file
jpeg("filename.jpg") JPEG file
pdf("filename.pdf") PDF file
png("filename.png") PNG file
postscript("filename.ps") PostScript file
svg("filename.svg") SVG file
win.metafile("filename.wmf") Windows metafile

Assume that you have three script files containing R code (script1.R, script2.R, and script3.R). Issuing the statement

source("script1.R")

submits the R code from script1.R to the current session, and the results appear on the screen.

If you then issue the statements

sink("myoutput", append=TRUE, split=TRUE)

pdf("mygraphs.pdf")

source("script2.R")

the R code from file script2.R is submitted, and the results again appear on the screen. In addition, the text output is appended to the file myoutput, and the graphic output is saved to the file mygraphs.pdf.

Finally, if you issue the statements

sink() dev.off() source("script3.R")

the R code from script3.R is submitted, and the results appear on the screen. This time, no text or graphic output is saved to files.

## 2.7 Using Help

Command Description
help.start() General help; manuals, packages, data import/export
help("mean") Help on function mean (or ?mean)
help.search("mean") Searches the help system for mean (or ??mean)
example("mean") Examples of function mean
RSiteSearch("mean") Searches the online help manuals and archived mailing lists for mean
data() Lists all available datasets contained in currently installed packages
vignette() Lists all available vignettes for currently installed packages
vignette("AER") Displays the vignette for AER package

The function help.start() opens a browser window with access to introductory and advanced manuals, FAQs, and reference materials. The RSiteSearch() function searches for a given topic in online help manuals and archives of the R-Help discussion list and returns the results in a browser window. The vignettes returned by the vignette() function are practical introductory articles provided in PDF format. Not all packages have vignettes.

1. Alternatively, = can also be used but in these notes I use <- as is the convention in R community.