## Functions to learn now

There are thousands of functions built into R. Below are just a few examples which are likely to be useful as you work with your data:

###### Repetition
# repeat something N times
rep("Apple pie", 10)
[1] "Apple pie" "Apple pie" "Apple pie" "Apple pie" "Apple pie"
[6] "Apple pie" "Apple pie" "Apple pie" "Apple pie" "Apple pie"
# repeat a short vector, combining into a single longer vector
rep(c("Custard", "Gravy"), 5)
[1] "Custard" "Gravy"   "Custard" "Gravy"   "Custard" "Gravy"   "Custard"
[8] "Gravy"   "Custard" "Gravy"  
###### Sequences
# make a sequence
(countdown <- 100:1)
[1] 100  99  98  97  96  95  94  93  92  91  90  89  88  87  86  85  84
[18]  83  82  81  80  79  78  77  76  75  74  73  72  71  70  69  68  67
[35]  66  65  64  63  62  61  60  59  58  57  56  55  54  53  52  51  50
[52]  49  48  47  46  45  44  43  42  41  40  39  38  37  36  35  34  33
[69]  32  31  30  29  28  27  26  25  24  23  22  21  20  19  18  17  16
[86]  15  14  13  12  11  10   9   8   7   6   5   4   3   2   1

Make sequences with steps of a particular size:

(tenths  <- seq(from=0, to=1, by=.1))
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

(twelfths <- seq(from=0, to=10, length.out=12))
[1]  0.0000000  0.9090909  1.8181818  2.7272727  3.6363636  4.5454545
[7]  5.4545455  6.3636364  7.2727273  8.1818182  9.0909091 10.0000000
###### Ranking
# generate some random data (here, ages in years)
ages <- round(rnorm(10, mean=40, sd=10))

# get the rank order of elements (i.e. what their positions would be if the vector was sorted)
ages
[1] 33 50 29 31 38 41 43 39 30 46
rank(ages, ties.method="first")
[1]  4 10  1  3  5  7  8  6  2  9
###### Unique values
# return the unique values in a vector
unique(rep(1:10, 100))
[1]  1  2  3  4  5  6  7  8  9 10
###### Lengths
# return the unique values in a vector
length(seq(1,100, 2))
[1] 50

Try and experiment with each of these functions. Check the output against what you expected to happen, and make sure you understand what they do.