The Binomial Distribution
0.0.1 Vectors
Vectors are the simplest type of object in R. There are 3 main types of vectors:
Numeric vectors Character vectors Logical vectors (Complex number vectors)
To set up a numeric vector x consisting of 5 numbers, 10.4, 5.6, 3.1, 6.4, 21.7,
We use the c() command to concatenate them
x = c(10.4, 5.6, 3.1, 6.4, 21.7)
To print the contents of x:
x
[1] 10.4 5.6 3.1 6.4 21.7
The [1] in front of the result is the index of the first element in the vector x.
To access a particular element of x
x[1] [1] 10.4
x[5] [1] 21.7
We can also do further assignments:
y <- c(x, 0, x)
This creates a vector y with 11 entries (two copies of x with a 0 in the middle)
In R Computations are performed element-wise, e.g. > 1/x
[1] 0.09615385 0.17857143 0.32258065 0.15625000 0.04608295
Short vectors are “recycled” to match long ones
v <- x + y
Warning message:
In x + y : longer object length is not a multiple of shorter object length
Some functions take vectors of values and produce results of the same length: sin, cos, tan, asin, acos, atan, log, exp, ……
cos(x) [1] -0.5609843 0.7755659 -0.9991352 0.9931849 -0.9579148
Some functions return a single value: sum, mean, max, min, prod, ……
sum(x) [1] 47.2
length(x) [1] 5
sum(x)/length(x) [1] 9.44
mean(x) [1] 9.44
Some other interesting functions cumsum, sort, range, pmax, pmin, : : :
####Generating Sequences R has a number of ways to generate sequences of numbers. These include the simplest approach, using the the colon “:”, e.g.
1:10 [1] 1 2 3 4 5 6 7 8 9 10
This operator has the highest priority within an expression, e.g. 21:10 is equivalent to 2(1:10).
The seq() function. (Use > ?seq to find out more about this function).
seq(1,10) seq(from=1, to=10) seq(to=10, from=1)
are all equivalent to 1:10.
We can also specify a step size (using by=value) or a length (using length=value) for the sequence.
s1 <- seq(1,10, by=0.5) s1 [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 [12] 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 s2 <- seq(1,10, length=5) s2 [1] 1.00 3.25 5.50 7.75 10.00
The rep() function replicates objects in various ways.
s3 <- rep(x, 2)
s3 [1] 10.4 5.6 3.1 6.4 21.7 10.4 5.6 3.1 6.4 21.7 [11] 10.4 5.6 3.1 6.4 21.7
s4 <- rep(c(1,4),c(10,15)) s4 [1] 1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
0.0.1.1 Character Vectors:
To set up a character/string vector z consisting of 4 place names use quotation marks.
z <- c(“Canberra”, “Sydney”, “Newcastle”, “Darwin”)
z <- c(
Canberra',
Sydney’,Newcastle',
Darwin’)
c(z, “Mary”, “John”)
[1] “Canberra” “Sydney” “Newcastle” “Darwin” “Mary” “John”
There are a lot of in-built functions in R to manipulate character vectors.
Logical Vectors A logical vector is a vector whose elements are TRUE, FALSE or NA. These can be generated by logical conditions, e.g.
temp <- x > 13
Takes each element of the vector x and compares it to 13. Returns a vector the same length as x, with a value TRUE when the condition is met and FALSE when it is not.
The logical operators are >, >=, <, <=, == for exact equality and != for inequality.
####Missing Values In some cases the entire contents of a vector may not be known. For example, missing data from a particular data set. A place can be reserved for this by assigning it the special value NA.
We can check for NA values in a vector x using the command > is.na(x)
This returns a logical vector the same length as x with a value TRUE if that particular element is NA.
w <- c(1:10, rep(NA,4), 22) is.na(w)
####Indexing Vectors We have already seen how to access single elements of a vector.
Subsets of a vector may also be selected using a similar approach.
ex1 <- w[!is.na(w)] Stores the elements of the vector w that do NOT have the value NA, into ex1.
ex2 <- w[1:3]
Selects the fi rst 3 elements of the vector w and stores them in the new vector ex2.
ex3 <- w[-(1:4)]
Using the - sign indicates that these elements should be excluded. This command excludes the first 4 elements of w.
ex4 <- w[-c(1,4)]
In this case only the 1st and 4th elements of w are excluded.
####Modifying Vectors
To alter the contents of a vector, similar methods can be used.
Remember x has contents > x [1] 10.4 5.6 3.1 6.4 21.7
For example, to modify the 1st element of x and assign it a value 5 use > x[1] <- 5 > x [1] 5.0 5.6 3.1 6.4 21.7
####Factors A factor is a special type of vector used to represent categorical data, e.g. gender, social class, etc.
Stored internally as a numeric vector with values 1, 2, ….. k, where k is the number of levels. Can have either ordered and unordered factors. A factor with k levels is stored internally consisting of 2 items (a) a vector of k integers (b) a character vector containing strings describing what the k levels are.
####Vectors: Data Manipulation
Types of vectors logical numeric character Others - Complex and Colour
creating a vector
scan()
X=c()
data.entry()
data manipulation sort() rev() Reverses the order of a vector rep(,n) Repeats a specified value or vector n times length() Returns the length of a vector order() Ordering of a vector
conversion and coercion
as.numeric() as.integer()
####Vectors: Indices