Interacting with R

What will happen when you enter these commands? Try it out.

1 + 2
2 * 3
4 ^ 5
6.7 / 8.9

Saving data in variables

a <- 10
a
##  10
b <- a + 11
b
##  21
c <- a / b
c
##  0.4761905

If you're working with very large numbers you can use scientific notation:

2e10
##  2e+10
2 * 10^10
##  2e+10
2e10 == 2 * 10^10
##  TRUE

Everything is a vector

You can see how many elements a vector holds using the length function:

length(10)
##  1
length(c)
##  1
length(1:10)
##  10

Composing vectors

c(1,2,3,4)
##  1 2 3 4
d <- c(5,6,7,8)
d + 10
##  15 16 17 18
d + d
##  10 12 14 16

Strings

To create strings, surround your text with either double " ... " or single ' ... ' quotes:

"a"
##  "a"
"a" == 'a'
##  TRUE
c( "a", "b", "c", "d" )
##  "a" "b" "c" "d"

Escape characters

s <- "My data are \"awesome\"!"
cat(s)
## My data are "awesome"!

Two other special string characters are tab \t and newline \n:

s <- "a\tb\tc"
cat(s)
## a    b   c
s <- "a\nb\nc"
cat(s)
## a
## b
## c

Boolean values

To create Boolean values use TRUE or FALSE:

TRUE
##  TRUE
FALSE
##  FALSE
TRUE == FALSE
##  FALSE

Missing Data

c(1, 2, NA, 4)
##   1  2 NA  4
c( "a", NA, "c", NA )
##  "a" NA  "c" NA
c(TRUE, FALSE, NA, FALSE)
##   TRUE FALSE    NA FALSE
is.na( c(1, NA) )
##  FALSE  TRUE

A note about NULL

NULL is used to signify unassigned variables:

NULL
## NULL
is.null(NULL)
##  TRUE

Extracting values

Let's say we have a vector of numbers:

myNumbers <- c( 10, 20, 30, 40, 50 )

We can extract elements from 1D vectors using the index syntax [] and integers:

myNumbers
##  10 20 30 40 50
myNumbers
##  10
myNumbers
##  30

We can use integer vectors with more than one element inside of our index [...]'s::

myNumbers[ c(1, 3) ]
##  10 30

You can use the : operator to easily create a sequence of numbers:

2:4
##  2 3 4
myNumbers[2:4]
##  20 30 40
myNumbers
##  10 20 30 40 50
myNumbers[ c(FALSE, TRUE,  TRUE,  TRUE,  TRUE ) ]
##  20 30 40 50
myNumbers[ c(TRUE,  FALSE, FALSE, FALSE, FALSE) ]
##  10

Logical operators always return a logical vector:

myNumbers > 25
##  FALSE FALSE  TRUE  TRUE  TRUE
myNumbers < 25
##   TRUE  TRUE FALSE FALSE FALSE
myNumbers == 30
##  FALSE FALSE  TRUE FALSE FALSE
myNumbers != 30
##   TRUE  TRUE FALSE  TRUE  TRUE

The %in% operator asks if the first set of numbers can be found in the second:

30 %in% myNumbers
##  TRUE
c(10, 100) %in% myNumbers
##   TRUE FALSE

The ! operator negates (flips) each value of a logical vector:

!TRUE
##  FALSE
!(myNumbers > 25)
##   TRUE  TRUE FALSE FALSE FALSE

So how can we combine logical comparisons with indexing?

myNumbers[myNumbers > 25]
##  30 40 50
myNumbers[myNumbers < 25]
##  10 20

You can get fancy…

myNumbers[ (myNumbers %% 2) == 0 ]
##  10 20 30 40 50

Assigning values

myNumbers
##  10 20 30 40 50
myNumbers    <- 100
myNumbers
##   10  20 100  40  50
myNumbers[2:3]  <- c(1,2)
myNumbers
##  10  1  2 40 50

Matrix

A matrix is a vector of vectors, each the same length and with the same type of data:

m <- matrix(1:8, nrow = 2, ncol = 4)
m
##      [,1] [,2] [,3] [,4]
## [1,]    1    3    5    7
## [2,]    2    4    6    8

You access values on a matrix by using a one element index, referring to a n'th position:

m
##  2

Alternatively you can specify a [row, col]:

m[1, 2]
##  3

Or just a row:

m[1, ]
##  1 3 5 7

Or just a column:

m[, 2]
##  3 4

If you forget this syntax, just pay attention to how R prints out matrices!

Array

array(1:8, dim = c(2, 2, 2))
## , , 1
##
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
##
## , , 2
##
##      [,1] [,2]
## [1,]    5    7
## [2,]    6    8

Lists

l <- list( a = c(1, 2, 3, 4), b = c("a", "b", "c") )
l
## $a ##  1 2 3 4 ## ##$b
##  "a" "b" "c"

You can access individual vectors on lists using indexing with numbers or names:

l
## $a ##  1 2 3 4 l["a"] ##$a
##  1 2 3 4

Did you notice what type of thing was returned there?

To simplify the result of indexing down to a vector (rather than a one element list):

l[]
##  1 2 3 4
l[["a"]]
##  1 2 3 4
l$a ##  1 2 3 4 l$a == l[["a"]]
##  TRUE TRUE TRUE TRUE

Make sure you understand why this fails:

l\$a == l["a"]