# The Simple Things...

## Type something in...

```> 33
```
```[1] 33
```
```> pi
```
```[1] 3.142
```
```> pi * 1.5
```
```[1] 4.712
```
```> 2 + 3 * 4^5
```
```[1] 3074
```
```> 2 + (3 * 4)^5
```
```[1] 248834
```

## Command Line

```\$ Rscript -e 'sqrt(123*234)'
[1] 169.6526
```

## Calling Functions

```> sqrt(2)/2
```
```[1] 0.7071
```
```> sin(45 * pi/180)
```
```[1] 0.7071
```

## Tests

```> 1 == 2
```
```[1] FALSE
```
```> 2 > 1
```
```[1] TRUE
```
```> sqrt(2)/2 == sin(45 * pi/180)
```
```[1] FALSE
```
```> sqrt(2)/2 - sin(45 * pi/180)
```
```[1] 1.11e-16
```

## Store values in named variables

```> a = sqrt(2)/2
> a <- sqrt(2)/2  # same thing
> a < -sqrt(2)/2  # not the same thing
```
```[1] FALSE
```
```> a
```
```[1] 0.7071
```
```> a == sqrt(2)/2
```
```[1] TRUE
```
```> b = a * sin(pi/5)
> b
```
```[1] 0.4156
```
```> a12.foo = pi/2
> a_foo = b * 99
```

## Vector values, vector arithmetic

```> a = c(1, 2, 3, 4, 5)
> a
```
```[1] 1 2 3 4 5
```
```> a + a
```
```[1]  2  4  6  8 10
```
```> a * 3
```
```[1]  3  6  9 12 15
```
```> a^4
```
```[1]   1  16  81 256 625
```

## Easy sequences

```> a = 4:12
> a
```
```[1]  4  5  6  7  8  9 10 11 12
```
```> b = seq(3, 27, len = 10)
> b
```
``` [1]  3.000  5.667  8.333 11.000 13.667 16.333 19.000
[8] 21.667 24.333 27.000
```
```> d = seq(3, 27, by = 1.3)
> d
```
``` [1]  3.0  4.3  5.6  6.9  8.2  9.5 10.8 12.1 13.4 14.7
[11] 16.0 17.3 18.6 19.9 21.2 22.5 23.8 25.1 26.4
```

## Elements

```> b
```
``` [1]  3.000  5.667  8.333 11.000 13.667 16.333 19.000
[8] 21.667 24.333 27.000
```
```> b[4]
```
```[1] 11
```
```> b[1239]
```
```[1] NA
```
```> b[-4]
```
```[1]  3.000  5.667  8.333 13.667 16.333 19.000 21.667
[8] 24.333 27.000
```
```> b[3, 6, 9]
```
```Error: incorrect number of dimensions
```
```> b[c(3, 6, 9)]
```
```[1]  8.333 16.333 24.333
```
```> b[0]
```
```numeric(0)
```
```> b[c(3, 0, 4)]
```
```[1]  8.333 11.000
```
```> b[]
```
``` [1]  3.000  5.667  8.333 11.000 13.667 16.333 19.000
[8] 21.667 24.333 27.000
```

## Chop and Slice

```> a[3:5]
```
```[1] 6 7 8
```
```> a[5:3]
```
```[1] 8 7 6
```
```> a[-(3:5)]
```
```[1]  4  5  9 10 11 12
```

## Variable Chops

```> slice = 3:5
> a[slice]
```
```[1] 6 7 8
```
```> b[slice]
```
```[1]  8.333 11.000 13.667
```
```> slice = slice + 1
> slice
```
```[1] 4 5 6
```
```> a[slice]
```
```[1] 7 8 9
```
```> b[slice]
```
```[1] 11.00 13.67 16.33
```

## Slicing for Truth

```> p = c(1, 1, 0, 1, 0, 0, 0)
> q = c(3, 5, 1, 6, 6, 5, 2)
> p == 1
```
```[1]  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE
```
```> q[p == 1]
```
```[1] 3 5 6
```

## Vector Functions

```> a
```
```[1]  4  5  6  7  8  9 10 11 12
```
```> shuffle = sample(a)
> shuffle
```
```[1]  9  5 12  8  7  4 11  6 10
```
```> diff(shuffle)
```
```[1] -4  7 -4 -1 -3  7 -5  4
```
```> sort(diff(shuffle))
```
```[1] -5 -4 -4 -3 -1  4  7  7
```
```> sum(a)
```
```[1] 72
```
```> cumsum(a)
```
```[1]  4  9 15 22 30 39 49 60 72
```
```> length(a)
```
```[1] 9
```

## Recycling

```>
> a = 1:8
> b = c(100, 200)
> a + b
```
```[1] 101 202 103 204 105 206 107 208
```
```> a = 1:7
> a + b
```
```Warning: longer object length is not a multiple of
shorter object length
```
```[1] 101 202 103 204 105 206 107
```

## Matrices

```> m = matrix(1:12, 3, 4)
> m
```
```     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
```
```> m = matrix(1:12, 3, 4, byrow = TRUE)
> m
```
```     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12
```
```> sqrt(m) * m + 3
```
```      [,1]   [,2]   [,3]  [,4]
[1,]  4.00  5.828  8.196 11.00
[2,] 14.18 17.697 21.520 25.63
[3,] 30.00 34.623 39.483 44.57
```

## Row and column names

```> rownames(m) = c("First", "Second", "Third")
> colnames(m) = c("a", "b", "c", "d")
> m
```
```       a  b  c  d
First  1  2  3  4
Second 5  6  7  8
Third  9 10 11 12
```

## Slicing Matrices

```> m[, 1]
```
``` First Second  Third
1      5      9
```
```> m[, 1, drop = FALSE]
```
```       a
First  1
Second 5
Third  9
```
```> m[1, ]
```
```a b c d
1 2 3 4
```
```> m[1, , drop = FALSE]
```
```      a b c d
First 1 2 3 4
```
```> m[-2, ]
```
```      a  b  c  d
First 1  2  3  4
Third 9 10 11 12
```
```> m[, 2:3]
```
```        b  c
First   2  3
Second  6  7
Third  10 11
```
```> m[, -4]
```
```       a  b  c
First  1  2  3
Second 5  6  7
Third  9 10 11
```

## Data Frames

```> d = data.frame(A = 1:10, B = sqrt(1:10), C = sample(10))
> d
```
```    A     B  C
1   1 1.000  6
2   2 1.414  7
3   3 1.732  2
4   4 2.000 10
5   5 2.236  8
6   6 2.449  3
7   7 2.646  4
8   8 2.828  9
9   9 3.000  5
10 10 3.162  1
```
```> d[1:5, ]  # get rows
```
```  A     B  C
1 1 1.000  6
2 2 1.414  7
3 3 1.732  2
4 4 2.000 10
5 5 2.236  8
```
```> d[, 1:2]  # get columns
```
```    A     B
1   1 1.000
2   2 1.414
3   3 1.732
4   4 2.000
5   5 2.236
6   6 2.449
7   7 2.646
8   8 2.828
9   9 3.000
10 10 3.162
```
```> d\$A * d\$B  # access by name
```
``` [1]  1.000  2.828  5.196  8.000 11.180 14.697 18.520
[8] 22.627 27.000 31.623
```
```> d\$C = d\$A * d\$B  # set by name
> d
```
```    A     B      C
1   1 1.000  1.000
2   2 1.414  2.828
3   3 1.732  5.196
4   4 2.000  8.000
5   5 2.236 11.180
6   6 2.449 14.697
7   7 2.646 18.520
8   8 2.828 22.627
9   9 3.000 27.000
10 10 3.162 31.623
```

## More than numbers

```> d\$BigB = d\$B > 2.5
> d\$Name = sample(letters, 10)
> d
```
```    A     B      C  BigB Name
1   1 1.000  1.000 FALSE    a
2   2 1.414  2.828 FALSE    g
3   3 1.732  5.196 FALSE    q
4   4 2.000  8.000 FALSE    w
5   5 2.236 11.180 FALSE    r
6   6 2.449 14.697 FALSE    j
7   7 2.646 18.520  TRUE    d
8   8 2.828 22.627  TRUE    c
9   9 3.000 27.000  TRUE    v
10 10 3.162 31.623  TRUE    u
```

## Lists

because information isn't always rectangular

```> LL = list(c(1, 5, 4, 3), c(2, 2, 2, 2, 2, 2, 2, 2))
> LL
```
```[[1]]
[1] 1 5 4 3

[[2]]
[1] 2 2 2 2 2 2 2 2
```
```> JJ = list(c("Fred", "Joe"), matrix(c(12, 13, 14, 15), 2,
+     2))
> JJ
```
```[[1]]
[1] "Fred" "Joe"

[[2]]
[,1] [,2]
[1,]   12   14
[2,]   13   15
```
```> KK = list(LL, JJ)
> KK
```
```[[1]]
[[1]][[1]]
[1] 1 5 4 3

[[1]][[2]]
[1] 2 2 2 2 2 2 2 2

[[2]]
[[2]][[1]]
[1] "Fred" "Joe"

[[2]][[2]]
[,1] [,2]
[1,]   12   14
[2,]   13   15
```

## De-listing

```> LL[[1]]
```
```[1] 1 5 4 3
```
```>
> NN = list(a = c(1, 2, 3), b = c("Hello", "World"))
> NN\$b
```
```[1] "Hello" "World"
```
```> NN[["b"]]
```
```[1] "Hello" "World"
```
```> NN[[2]]
```
```[1] "Hello" "World"
```
```>
```

## What Have I Created?

```> ls()
```
``` [1] "JJ"      "KK"      "LL"      "NN"      "a"
[6] "a12.foo" "a_foo"   "b"       "d"       "inFile"
[11] "m"       "outFile" "p"       "q"       "shuffle"
[16] "slice"
```
```> ls(pat = "a.*")
```
```[1] "a"       "a12.foo" "a_foo"
```
```> length(ls())
```
```[1] 16
```

## Saving

```> save.image()
```

Happens when you quit: `q()`