Tutorial by Examples

Use the : operator to create sequences of numbers, such as for use in vectorizing larger chunks of your code: x <- 1:5 x ## [1] 1 2 3 4 5 This works both ways 10:4 # [1] 10 9 8 7 6 5 4 and even with floating point numbers 1.25:5 # [1] 1.25 2.25 3.25 4.25 or negatives -4:4 ...
seq is a more flexible function than the : operator allowing to specify steps other than 1. The function creates a sequence from the start (default is 1) to the end including that number. You can supply only the end (to) parameter seq(5) # [1] 1 2 3 4 5 As well as the start seq(2, 5) # or se...
Vectors in R can have different types (e.g. integer, logical, character). The most general way of defining a vector is by using the function vector(). vector('integer',2) # creates a vector of integers of size 2. vector('character',2) # creates a vector of characters of size 2. vector('logical',2...
Named vector can be created in several ways. With c: xc <- c('a' = 5, 'b' = 6, 'c' = 7, 'd' = 8) which results in: > xc a b c d 5 6 7 8 with list: xl <- list('a' = 5, 'b' = 6, 'c' = 7, 'd' = 8) which results in: > xl $a [1] 5 $b [1] 6 $c [1] 7 $d [1] 8 Wi...
The rep function can be used to repeat a vector in a fairly flexible manner. # repeat counting numbers, 1 through 5 twice rep(1:5, 2) [1] 1 2 3 4 5 1 2 3 4 5 # repeat vector with incomplete recycling rep(1:5, 2, length.out=7) [1] 1 2 3 4 5 1 2 The each argument is especially useful for ex...
R has a number of build in constants. The following constants are available: LETTERS: the 26 upper-case letters of the Roman alphabet letters: the 26 lower-case letters of the Roman alphabet month.abb: the three-letter abbreviations for the English month names month.name: the English names fo...

Page 1 of 1