R Basics

Data Classes

  • Numeric - numbers (e.g. 1, 3.673)
  • Character - strings or words ("hey", "I'm a string") in either single or double quotes
  • Logicals - TRUE or FALSE - all capital letters and are not in quotes.

Data Types

  • vector - 1-dimensional object of one class (all numeric or all character)
  • matrix - 2-dimensional object of one class
  • data.frame - 2-dimensional object, can be multiple classes (like Excel spreadsheet)
  • array - object of > 2 dimensions of one class. The data in a nifti object is one of these (usually 3-D)

Initializing: vectors

  • Use c() to create a vector of numeric values:
v = c(1, 4, 3, 7, 8)
print(v)
[1] 1 4 3 7 8
  • Shortcut for sequential numeric vector:
w = 1:5
print(w)
[1] 1 2 3 4 5

The gray boxes denote code, and the lines after denote the output.

Assignment

In R, you can assign using the equals = or arrow <- (aka assigment operator).

The above commands are equivalent to:

w = 1:5
w <- 1:5

There are no differences in these 2 commands, but just preference (we use =).

Variable/object names:

  • must start with a letter
  • cannot contain spaces, $, quotes, or other special characters
    • generally just alpha-numeric
  • can contain periods (.) and underscores _

Help

  • To see the documentation for a function, use the ? symbol before the name of the function. This is a shortcut for the help command:
  • For example, to see documentation for c:
?c
help(topic = "c")
  • To search for help files, use a double ?? or help.search:
??c
help.search(pattern = "c")

Some Details

  • R is case sensitive (e.g. y and Y are different)
  • Commands separated by new line or by a colon (;)
  • Use # to comment

You can also be explicit about which package you are using with the :: operator, where the syntax is package::function():

utils::help("c")

Initializing: matrices and arrays

  • Create a 3 x 4 numeric matrix and assign to variable m
    • note items are added column-wise
m = matrix(1:12, nrow = 3)
print(m)
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
  • Create a 3 x 4 x 3 numeric array and assign to variable a
a = array(1:36, dim = c(3, 4, 3))
  • the dim() function returns the dimensions of the array
dim(a)
[1] 3 4 3

Subsetting: vectors

  • Subsetting a vector (first index is 1, not zero):
print(v)
[1] 1 4 3 7 8
print(v[4])
[1] 7
print(v[1:3])
[1] 1 4 3
print(v[c(1,3,5)])
[1] 1 3 8

Subsetting: matrices

  • Subsetting a matrix - [row,column] format,
print(m[1,3])
[1] 7
print(m[1:2,3:4])
     [,1] [,2]
[1,]    7   10
[2,]    8   11
  • if row or column missing then all values printed:
print(m[,4])
[1] 10 11 12
print(m[2,])
[1]  2  5  8 11

Subsetting: arrays

  • Subsetting - [x,y,z] format:
print(a[1,1,1])
[1] 1
dim(a[,4,])
[1] 3 3

This will return an error - need to specify all dims:

a[,4]

Operators in R: return numeric

  • Arithmetic: +, -, *, /, ^ - exponents
  • Standard math functions: log, abs, sqrt
print(v); print(w)
[1] 1 4 3 7 8
[1] 1 2 3 4 5
print(v + 4)
[1]  5  8  7 11 12
print(v + w)
[1]  2  6  6 11 13
print(sqrt(w^2))
[1] 1 2 3 4 5

Operators in R: return logical

  • Comparison: >, >=, <, <=, == (equals), != (not equal)
  • Logical: ! - not, & - and, | - or (a “pipe”)
  • all(): function to test all values TRUE and any(): (are any)
print(!FALSE)
[1] TRUE
print(TRUE | FALSE)
[1] TRUE
print(FALSE & FALSE)
[1] FALSE
c(all(c(TRUE, FALSE)), any(c(TRUE, FALSE)))
[1] FALSE  TRUE

Subsetting with logicals

The which command takes a logical and gets the indices of TRUE:

which(v > 5)
[1] 4 5
v[ which(v > 5) ]
[1] 7 8

Or directly pass in a vector of logicals to subset:

v[ v > 5 ]
[1] 7 8

This method will be useful later when we are working with images.

Website