You are on page 1of 71

Lecture 2: Statistical Computing

Simple Manipulations: Numbers and Vectors

Ms. Beryl Ang’iro

August 21, 2020

Ms. Beryl Ang’iro August 21, 2020 1 / 12


Simple Manipulations
Vectors and assignment

Vectors and assignment


R operates on named data structures. The simplest such structure is the
vector, which is a single entity consisting of an ordered collection of
numbers.

The concatenation command c() is used to set up a named vector


consisting of numbers. e.g.

> x <- c(10.4,5.6,3.1,6.4,21.7).

This is an assignment statement. The function c() which in this context


can take an arbitrary number of arguments evaluates to a vector by
concatenating its arguments from end to end.

Note: A number occurring by itself in an expression is taken as a vector of


length one.
Ms. Beryl Ang’iro August 21, 2020 2 / 12
Simple Manipulations
Vectors and assignment

Vectors and assignment


R operates on named data structures. The simplest such structure is the
vector, which is a single entity consisting of an ordered collection of
numbers.

The concatenation command c() is used to set up a named vector


consisting of numbers. e.g.

> x <- c(10.4,5.6,3.1,6.4,21.7).

This is an assignment statement. The function c() which in this context


can take an arbitrary number of arguments evaluates to a vector by
concatenating its arguments from end to end.

Note: A number occurring by itself in an expression is taken as a vector of


length one.
Ms. Beryl Ang’iro August 21, 2020 2 / 12
Simple Manipulations
Vectors and assignment

Vectors and assignment


R operates on named data structures. The simplest such structure is the
vector, which is a single entity consisting of an ordered collection of
numbers.

The concatenation command c() is used to set up a named vector


consisting of numbers. e.g.

> x <- c(10.4,5.6,3.1,6.4,21.7).

This is an assignment statement. The function c() which in this context


can take an arbitrary number of arguments evaluates to a vector by
concatenating its arguments from end to end.

Note: A number occurring by itself in an expression is taken as a vector of


length one.
Ms. Beryl Ang’iro August 21, 2020 2 / 12
Simple Manipulations
Vectors and assignment

Vectors and assignment


R operates on named data structures. The simplest such structure is the
vector, which is a single entity consisting of an ordered collection of
numbers.

The concatenation command c() is used to set up a named vector


consisting of numbers. e.g.

> x <- c(10.4,5.6,3.1,6.4,21.7).

This is an assignment statement. The function c() which in this context


can take an arbitrary number of arguments evaluates to a vector by
concatenating its arguments from end to end.

Note: A number occurring by itself in an expression is taken as a vector of


length one.
Ms. Beryl Ang’iro August 21, 2020 2 / 12
Simple Manipulations
Vectors and assignment

Vectors and assignment


R operates on named data structures. The simplest such structure is the
vector, which is a single entity consisting of an ordered collection of
numbers.

The concatenation command c() is used to set up a named vector


consisting of numbers. e.g.

> x <- c(10.4,5.6,3.1,6.4,21.7).

This is an assignment statement. The function c() which in this context


can take an arbitrary number of arguments evaluates to a vector by
concatenating its arguments from end to end.

Note: A number occurring by itself in an expression is taken as a vector of


length one.
Ms. Beryl Ang’iro August 21, 2020 2 / 12
Simple Manipulations
Vectors and assignment

An assignment can also be performed with the use of the assign()


command, hence the previous assignment to x can equivalently be;
> assign("x", c(10.4,5.6,3.1,6.4,21.7))
The same assignment can also be obtained as follows;
> c(10.4,5.6,3.1,6.4,21.7)<- x
Further assignments are;
> x1
This computes the reciprocals of the five values and prints them out on
the screen (the value of x remains unchanged)
> y <- c(x,0,x)
creates a vector with 11 entries consisting of two copies of x with a zero in
the middle place and assign them to y.

Ms. Beryl Ang’iro August 21, 2020 3 / 12


Simple Manipulations
Vectors and assignment

An assignment can also be performed with the use of the assign()


command, hence the previous assignment to x can equivalently be;
> assign("x", c(10.4,5.6,3.1,6.4,21.7))
The same assignment can also be obtained as follows;
> c(10.4,5.6,3.1,6.4,21.7)<- x
Further assignments are;
> x1
This computes the reciprocals of the five values and prints them out on
the screen (the value of x remains unchanged)
> y <- c(x,0,x)
creates a vector with 11 entries consisting of two copies of x with a zero in
the middle place and assign them to y.

Ms. Beryl Ang’iro August 21, 2020 3 / 12


Simple Manipulations
Vectors and assignment

An assignment can also be performed with the use of the assign()


command, hence the previous assignment to x can equivalently be;
> assign("x", c(10.4,5.6,3.1,6.4,21.7))
The same assignment can also be obtained as follows;
> c(10.4,5.6,3.1,6.4,21.7)<- x
Further assignments are;
> x1
This computes the reciprocals of the five values and prints them out on
the screen (the value of x remains unchanged)
> y <- c(x,0,x)
creates a vector with 11 entries consisting of two copies of x with a zero in
the middle place and assign them to y.

Ms. Beryl Ang’iro August 21, 2020 3 / 12


Simple Manipulations
Vectors and assignment

An assignment can also be performed with the use of the assign()


command, hence the previous assignment to x can equivalently be;
> assign("x", c(10.4,5.6,3.1,6.4,21.7))
The same assignment can also be obtained as follows;
> c(10.4,5.6,3.1,6.4,21.7)<- x
Further assignments are;
> x1
This computes the reciprocals of the five values and prints them out on
the screen (the value of x remains unchanged)
> y <- c(x,0,x)
creates a vector with 11 entries consisting of two copies of x with a zero in
the middle place and assign them to y.

Ms. Beryl Ang’iro August 21, 2020 3 / 12


Simple Manipulations
Vectors and assignment

An assignment can also be performed with the use of the assign()


command, hence the previous assignment to x can equivalently be;
> assign("x", c(10.4,5.6,3.1,6.4,21.7))
The same assignment can also be obtained as follows;
> c(10.4,5.6,3.1,6.4,21.7)<- x
Further assignments are;
> x1
This computes the reciprocals of the five values and prints them out on
the screen (the value of x remains unchanged)
> y <- c(x,0,x)
creates a vector with 11 entries consisting of two copies of x with a zero in
the middle place and assign them to y.

Ms. Beryl Ang’iro August 21, 2020 3 / 12


Simple Manipulations
Vectors and assignment

An assignment can also be performed with the use of the assign()


command, hence the previous assignment to x can equivalently be;
> assign("x", c(10.4,5.6,3.1,6.4,21.7))
The same assignment can also be obtained as follows;
> c(10.4,5.6,3.1,6.4,21.7)<- x
Further assignments are;
> x1
This computes the reciprocals of the five values and prints them out on
the screen (the value of x remains unchanged)
> y <- c(x,0,x)
creates a vector with 11 entries consisting of two copies of x with a zero in
the middle place and assign them to y.

Ms. Beryl Ang’iro August 21, 2020 3 / 12


Simple Manipulations
Vectors and assignment

An assignment can also be performed with the use of the assign()


command, hence the previous assignment to x can equivalently be;
> assign("x", c(10.4,5.6,3.1,6.4,21.7))
The same assignment can also be obtained as follows;
> c(10.4,5.6,3.1,6.4,21.7)<- x
Further assignments are;
> x1
This computes the reciprocals of the five values and prints them out on
the screen (the value of x remains unchanged)
> y <- c(x,0,x)
creates a vector with 11 entries consisting of two copies of x with a zero in
the middle place and assign them to y.

Ms. Beryl Ang’iro August 21, 2020 3 / 12


Simple Manipulations
Vector Arithmetic

Vector Arithmetic

Vectors can be used in arithmetic expressions, in which case the operations


are performed element by element.
Vectors occurring in the same expression do not all need to be of the same
length. If they are not, the shorter vectors are recycled as often as needed
(perhaps fractionally) until they match the length of the longest vector.
For example with the above assignments, the further assignment
> v <- 2*x+y+1
generates a new vector v of length 11 constructed by adding together ,
element by element, 2 ∗ x repeated 2 times, y just once and 1 repeated 11
times.
The elementary arithmetic operators are the usual +, −, ∗, / and ∧ for
raising to a power.
Ms. Beryl Ang’iro August 21, 2020 4 / 12
Simple Manipulations
Vector Arithmetic

Vector Arithmetic

Vectors can be used in arithmetic expressions, in which case the operations


are performed element by element.
Vectors occurring in the same expression do not all need to be of the same
length. If they are not, the shorter vectors are recycled as often as needed
(perhaps fractionally) until they match the length of the longest vector.
For example with the above assignments, the further assignment
> v <- 2*x+y+1
generates a new vector v of length 11 constructed by adding together ,
element by element, 2 ∗ x repeated 2 times, y just once and 1 repeated 11
times.
The elementary arithmetic operators are the usual +, −, ∗, / and ∧ for
raising to a power.
Ms. Beryl Ang’iro August 21, 2020 4 / 12
Simple Manipulations
Vector Arithmetic

Vector Arithmetic

Vectors can be used in arithmetic expressions, in which case the operations


are performed element by element.
Vectors occurring in the same expression do not all need to be of the same
length. If they are not, the shorter vectors are recycled as often as needed
(perhaps fractionally) until they match the length of the longest vector.
For example with the above assignments, the further assignment
> v <- 2*x+y+1
generates a new vector v of length 11 constructed by adding together ,
element by element, 2 ∗ x repeated 2 times, y just once and 1 repeated 11
times.
The elementary arithmetic operators are the usual +, −, ∗, / and ∧ for
raising to a power.
Ms. Beryl Ang’iro August 21, 2020 4 / 12
Simple Manipulations
Vector Arithmetic

Vector Arithmetic

Vectors can be used in arithmetic expressions, in which case the operations


are performed element by element.
Vectors occurring in the same expression do not all need to be of the same
length. If they are not, the shorter vectors are recycled as often as needed
(perhaps fractionally) until they match the length of the longest vector.
For example with the above assignments, the further assignment
> v <- 2*x+y+1
generates a new vector v of length 11 constructed by adding together ,
element by element, 2 ∗ x repeated 2 times, y just once and 1 repeated 11
times.
The elementary arithmetic operators are the usual +, −, ∗, / and ∧ for
raising to a power.
Ms. Beryl Ang’iro August 21, 2020 4 / 12
Simple Manipulations
Vector Arithmetic

Vector Arithmetic

Vectors can be used in arithmetic expressions, in which case the operations


are performed element by element.
Vectors occurring in the same expression do not all need to be of the same
length. If they are not, the shorter vectors are recycled as often as needed
(perhaps fractionally) until they match the length of the longest vector.
For example with the above assignments, the further assignment
> v <- 2*x+y+1
generates a new vector v of length 11 constructed by adding together ,
element by element, 2 ∗ x repeated 2 times, y just once and 1 repeated 11
times.
The elementary arithmetic operators are the usual +, −, ∗, / and ∧ for
raising to a power.
Ms. Beryl Ang’iro August 21, 2020 4 / 12
Simple Manipulations
Vector Arithmetic

In addition all of the common arithmetic functions are available log(),


exp(), sin(), cos(),sqrt() and so on, all have their usual meaning.
Other functions include;
• max() - for selecting the largest element of a vector. e.g.
> max(x)
[1] 21.7
• min() - for selecting the smallest element of a vector. e.g.
> min(x)
[1] 3.1
• range() - a function whose value is a vector of length two, composed
of the minimum and the maximum. e.g.
> range(x)
[1] 3.1 21.7

Ms. Beryl Ang’iro August 21, 2020 5 / 12


Simple Manipulations
Vector Arithmetic

In addition all of the common arithmetic functions are available log(),


exp(), sin(), cos(),sqrt() and so on, all have their usual meaning.
Other functions include;
• max() - for selecting the largest element of a vector. e.g.
> max(x)
[1] 21.7
• min() - for selecting the smallest element of a vector. e.g.
> min(x)
[1] 3.1
• range() - a function whose value is a vector of length two, composed
of the minimum and the maximum. e.g.
> range(x)
[1] 3.1 21.7

Ms. Beryl Ang’iro August 21, 2020 5 / 12


Simple Manipulations
Vector Arithmetic

In addition all of the common arithmetic functions are available log(),


exp(), sin(), cos(),sqrt() and so on, all have their usual meaning.
Other functions include;
• max() - for selecting the largest element of a vector. e.g.
> max(x)
[1] 21.7
• min() - for selecting the smallest element of a vector. e.g.
> min(x)
[1] 3.1
• range() - a function whose value is a vector of length two, composed
of the minimum and the maximum. e.g.
> range(x)
[1] 3.1 21.7

Ms. Beryl Ang’iro August 21, 2020 5 / 12


Simple Manipulations
Vector Arithmetic

In addition all of the common arithmetic functions are available log(),


exp(), sin(), cos(),sqrt() and so on, all have their usual meaning.
Other functions include;
• max() - for selecting the largest element of a vector. e.g.
> max(x)
[1] 21.7
• min() - for selecting the smallest element of a vector. e.g.
> min(x)
[1] 3.1
• range() - a function whose value is a vector of length two, composed
of the minimum and the maximum. e.g.
> range(x)
[1] 3.1 21.7

Ms. Beryl Ang’iro August 21, 2020 5 / 12


Simple Manipulations
Vector Arithmetic

In addition all of the common arithmetic functions are available log(),


exp(), sin(), cos(),sqrt() and so on, all have their usual meaning.
Other functions include;
• max() - for selecting the largest element of a vector. e.g.
> max(x)
[1] 21.7
• min() - for selecting the smallest element of a vector. e.g.
> min(x)
[1] 3.1
• range() - a function whose value is a vector of length two, composed
of the minimum and the maximum. e.g.
> range(x)
[1] 3.1 21.7

Ms. Beryl Ang’iro August 21, 2020 5 / 12


Simple Manipulations
Vector Arithmetic

In addition all of the common arithmetic functions are available log(),


exp(), sin(), cos(),sqrt() and so on, all have their usual meaning.
Other functions include;
• max() - for selecting the largest element of a vector. e.g.
> max(x)
[1] 21.7
• min() - for selecting the smallest element of a vector. e.g.
> min(x)
[1] 3.1
• range() - a function whose value is a vector of length two, composed
of the minimum and the maximum. e.g.
> range(x)
[1] 3.1 21.7

Ms. Beryl Ang’iro August 21, 2020 5 / 12


Simple Manipulations
Vector Arithmetic

In addition all of the common arithmetic functions are available log(),


exp(), sin(), cos(),sqrt() and so on, all have their usual meaning.
Other functions include;
• max() - for selecting the largest element of a vector. e.g.
> max(x)
[1] 21.7
• min() - for selecting the smallest element of a vector. e.g.
> min(x)
[1] 3.1
• range() - a function whose value is a vector of length two, composed
of the minimum and the maximum. e.g.
> range(x)
[1] 3.1 21.7

Ms. Beryl Ang’iro August 21, 2020 5 / 12


Simple Manipulations
Vector Arithmetic

• length(x) - is the number of elements in x. e.g


> length(x)
[1] 5
• sum(x) - gives the total of the elements in x. e.g
> sum(x)
[1] 47.2
• prod(x) - gives the product of the elements in x. e.g
> prod(x)
[1] 25073.95

Ms. Beryl Ang’iro August 21, 2020 6 / 12


Simple Manipulations
Vector Arithmetic

• length(x) - is the number of elements in x. e.g


> length(x)
[1] 5
• sum(x) - gives the total of the elements in x. e.g
> sum(x)
[1] 47.2
• prod(x) - gives the product of the elements in x. e.g
> prod(x)
[1] 25073.95

Ms. Beryl Ang’iro August 21, 2020 6 / 12


Simple Manipulations
Vector Arithmetic

• length(x) - is the number of elements in x. e.g


> length(x)
[1] 5
• sum(x) - gives the total of the elements in x. e.g
> sum(x)
[1] 47.2
• prod(x) - gives the product of the elements in x. e.g
> prod(x)
[1] 25073.95

Ms. Beryl Ang’iro August 21, 2020 6 / 12


Simple Manipulations
Vector Arithmetic

• length(x) - is the number of elements in x. e.g


> length(x)
[1] 5
• sum(x) - gives the total of the elements in x. e.g
> sum(x)
[1] 47.2
• prod(x) - gives the product of the elements in x. e.g
> prod(x)
[1] 25073.95

Ms. Beryl Ang’iro August 21, 2020 6 / 12


Simple Manipulations
Vector Arithmetic

• length(x) - is the number of elements in x. e.g


> length(x)
[1] 5
• sum(x) - gives the total of the elements in x. e.g
> sum(x)
[1] 47.2
• prod(x) - gives the product of the elements in x. e.g
> prod(x)
[1] 25073.95

Ms. Beryl Ang’iro August 21, 2020 6 / 12


Simple Manipulations
Vector Arithmetic

• length(x) - is the number of elements in x. e.g


> length(x)
[1] 5
• sum(x) - gives the total of the elements in x. e.g
> sum(x)
[1] 47.2
• prod(x) - gives the product of the elements in x. e.g
> prod(x)
[1] 25073.95

Ms. Beryl Ang’iro August 21, 2020 6 / 12


Simple Manipulations
Vector Arithmetic

• mean(x) - calculates the mean of x (which


P is the same as
sum(x)/length(x) from the formula x̄ = (x)
n ). e.g
> mean(x)
[1] 9.44
or
> sum(x)/length(x)
• var(x) - calculates the variance of x (which is equivalent to
(x−x̄)2
P
sum((x−mean(x))∧ 2/(length(x)−1) from the formula s 2 =
n−1 ) e.g
> var(x)
[1] 53.853
or
> variance <- sum((x−mean(x))∧ 2/(length(x)−1)
The standard deviation is the square root of variance, given as;
> sqrt(variance)
[1] 7.33846
Ms. Beryl Ang’iro August 21, 2020 7 / 12
Simple Manipulations
Vector Arithmetic

• mean(x) - calculates the mean of x (which


P is the same as
sum(x)/length(x) from the formula x̄ = (x)
n ). e.g
> mean(x)
[1] 9.44
or
> sum(x)/length(x)
• var(x) - calculates the variance of x (which is equivalent to
(x−x̄)2
P
sum((x−mean(x))∧ 2/(length(x)−1) from the formula s 2 =
n−1 ) e.g
> var(x)
[1] 53.853
or
> variance <- sum((x−mean(x))∧ 2/(length(x)−1)
The standard deviation is the square root of variance, given as;
> sqrt(variance)
[1] 7.33846
Ms. Beryl Ang’iro August 21, 2020 7 / 12
Simple Manipulations
Vector Arithmetic

• mean(x) - calculates the mean of x (which


P is the same as
sum(x)/length(x) from the formula x̄ = (x)
n ). e.g
> mean(x)
[1] 9.44
or
> sum(x)/length(x)
• var(x) - calculates the variance of x (which is equivalent to
(x−x̄)2
P
sum((x−mean(x))∧ 2/(length(x)−1) from the formula s 2 =
n−1 ) e.g
> var(x)
[1] 53.853
or
> variance <- sum((x−mean(x))∧ 2/(length(x)−1)
The standard deviation is the square root of variance, given as;
> sqrt(variance)
[1] 7.33846
Ms. Beryl Ang’iro August 21, 2020 7 / 12
Simple Manipulations
Vector Arithmetic

• mean(x) - calculates the mean of x (which


P is the same as
sum(x)/length(x) from the formula x̄ = (x)
n ). e.g
> mean(x)
[1] 9.44
or
> sum(x)/length(x)
• var(x) - calculates the variance of x (which is equivalent to
(x−x̄)2
P
sum((x−mean(x))∧ 2/(length(x)−1) from the formula s 2 =
n−1 ) e.g
> var(x)
[1] 53.853
or
> variance <- sum((x−mean(x))∧ 2/(length(x)−1)
The standard deviation is the square root of variance, given as;
> sqrt(variance)
[1] 7.33846
Ms. Beryl Ang’iro August 21, 2020 7 / 12
Simple Manipulations
Vector Arithmetic

• mean(x) - calculates the mean of x (which


P is the same as
sum(x)/length(x) from the formula x̄ = (x)
n ). e.g
> mean(x)
[1] 9.44
or
> sum(x)/length(x)
• var(x) - calculates the variance of x (which is equivalent to
(x−x̄)2
P
sum((x−mean(x))∧ 2/(length(x)−1) from the formula s 2 =
n−1 ) e.g
> var(x)
[1] 53.853
or
> variance <- sum((x−mean(x))∧ 2/(length(x)−1)
The standard deviation is the square root of variance, given as;
> sqrt(variance)
[1] 7.33846
Ms. Beryl Ang’iro August 21, 2020 7 / 12
Simple Manipulations
Vector Arithmetic

• mean(x) - calculates the mean of x (which


P is the same as
sum(x)/length(x) from the formula x̄ = (x)
n ). e.g
> mean(x)
[1] 9.44
or
> sum(x)/length(x)
• var(x) - calculates the variance of x (which is equivalent to
(x−x̄)2
P
sum((x−mean(x))∧ 2/(length(x)−1) from the formula s 2 =
n−1 ) e.g
> var(x)
[1] 53.853
or
> variance <- sum((x−mean(x))∧ 2/(length(x)−1)
The standard deviation is the square root of variance, given as;
> sqrt(variance)
[1] 7.33846
Ms. Beryl Ang’iro August 21, 2020 7 / 12
Simple Manipulations
Vector Arithmetic

• mean(x) - calculates the mean of x (which


P is the same as
sum(x)/length(x) from the formula x̄ = (x)
n ). e.g
> mean(x)
[1] 9.44
or
> sum(x)/length(x)
• var(x) - calculates the variance of x (which is equivalent to
(x−x̄)2
P
sum((x−mean(x))∧ 2/(length(x)−1) from the formula s 2 =
n−1 ) e.g
> var(x)
[1] 53.853
or
> variance <- sum((x−mean(x))∧ 2/(length(x)−1)
The standard deviation is the square root of variance, given as;
> sqrt(variance)
[1] 7.33846
Ms. Beryl Ang’iro August 21, 2020 7 / 12
Simple Manipulations
Generating regular sequences

Generating regular sequences


R has a number of facilities for generating commonly used sequences of
numbers;
Using the colon operator:
For example, for a sequence of numbers from 1 to 30 we have;
> 1:30
generates the vector c(1,2,3,...,28,29,30).
The colon operator has highest priority within an expression, e.g,
> 2*1:15
generates the vector c(2,4,6,...,28,30)
Put
> n <- 10
and compare the sequences
> 1:n-1
and
> 1:(n-1)
Ms. Beryl Ang’iro August 21, 2020 8 / 12
Simple Manipulations
Generating regular sequences

Generating regular sequences


R has a number of facilities for generating commonly used sequences of
numbers;
Using the colon operator:
For example, for a sequence of numbers from 1 to 30 we have;
> 1:30
generates the vector c(1,2,3,...,28,29,30).
The colon operator has highest priority within an expression, e.g,
> 2*1:15
generates the vector c(2,4,6,...,28,30)
Put
> n <- 10
and compare the sequences
> 1:n-1
and
> 1:(n-1)
Ms. Beryl Ang’iro August 21, 2020 8 / 12
Simple Manipulations
Generating regular sequences

Generating regular sequences


R has a number of facilities for generating commonly used sequences of
numbers;
Using the colon operator:
For example, for a sequence of numbers from 1 to 30 we have;
> 1:30
generates the vector c(1,2,3,...,28,29,30).
The colon operator has highest priority within an expression, e.g,
> 2*1:15
generates the vector c(2,4,6,...,28,30)
Put
> n <- 10
and compare the sequences
> 1:n-1
and
> 1:(n-1)
Ms. Beryl Ang’iro August 21, 2020 8 / 12
Simple Manipulations
Generating regular sequences

Generating regular sequences


R has a number of facilities for generating commonly used sequences of
numbers;
Using the colon operator:
For example, for a sequence of numbers from 1 to 30 we have;
> 1:30
generates the vector c(1,2,3,...,28,29,30).
The colon operator has highest priority within an expression, e.g,
> 2*1:15
generates the vector c(2,4,6,...,28,30)
Put
> n <- 10
and compare the sequences
> 1:n-1
and
> 1:(n-1)
Ms. Beryl Ang’iro August 21, 2020 8 / 12
Simple Manipulations
Generating regular sequences

Generating regular sequences


R has a number of facilities for generating commonly used sequences of
numbers;
Using the colon operator:
For example, for a sequence of numbers from 1 to 30 we have;
> 1:30
generates the vector c(1,2,3,...,28,29,30).
The colon operator has highest priority within an expression, e.g,
> 2*1:15
generates the vector c(2,4,6,...,28,30)
Put
> n <- 10
and compare the sequences
> 1:n-1
and
> 1:(n-1)
Ms. Beryl Ang’iro August 21, 2020 8 / 12
Simple Manipulations
Generating regular sequences

Generating regular sequences


R has a number of facilities for generating commonly used sequences of
numbers;
Using the colon operator:
For example, for a sequence of numbers from 1 to 30 we have;
> 1:30
generates the vector c(1,2,3,...,28,29,30).
The colon operator has highest priority within an expression, e.g,
> 2*1:15
generates the vector c(2,4,6,...,28,30)
Put
> n <- 10
and compare the sequences
> 1:n-1
and
> 1:(n-1)
Ms. Beryl Ang’iro August 21, 2020 8 / 12
Simple Manipulations
Generating regular sequences

Generating regular sequences


R has a number of facilities for generating commonly used sequences of
numbers;
Using the colon operator:
For example, for a sequence of numbers from 1 to 30 we have;
> 1:30
generates the vector c(1,2,3,...,28,29,30).
The colon operator has highest priority within an expression, e.g,
> 2*1:15
generates the vector c(2,4,6,...,28,30)
Put
> n <- 10
and compare the sequences
> 1:n-1
and
> 1:(n-1)
Ms. Beryl Ang’iro August 21, 2020 8 / 12
Simple Manipulations
Generating regular sequences

The seq() function

This is a more general function for generating sequences. It has five


arguments, only some of which may be specified in any one call.

The first two arguments specify the beginning and end of the sequence,
and if these are the only two arguments given, the result is the same as
the colon operator i.e seq(2,10) is the same vector as 2:10.

The parameters to seq(), and to many other functions can also be given
in named form, in which case the order in which they appear is irrelevant.

The first two parameters may be named from=value and to=value; thus
seq(1,30) and seq(from=1,to=30) and seq(to=30,from=1) are all
the same as 1:30

Ms. Beryl Ang’iro August 21, 2020 9 / 12


Simple Manipulations
Generating regular sequences

The seq() function

This is a more general function for generating sequences. It has five


arguments, only some of which may be specified in any one call.

The first two arguments specify the beginning and end of the sequence,
and if these are the only two arguments given, the result is the same as
the colon operator i.e seq(2,10) is the same vector as 2:10.

The parameters to seq(), and to many other functions can also be given
in named form, in which case the order in which they appear is irrelevant.

The first two parameters may be named from=value and to=value; thus
seq(1,30) and seq(from=1,to=30) and seq(to=30,from=1) are all
the same as 1:30

Ms. Beryl Ang’iro August 21, 2020 9 / 12


Simple Manipulations
Generating regular sequences

The seq() function

This is a more general function for generating sequences. It has five


arguments, only some of which may be specified in any one call.

The first two arguments specify the beginning and end of the sequence,
and if these are the only two arguments given, the result is the same as
the colon operator i.e seq(2,10) is the same vector as 2:10.

The parameters to seq(), and to many other functions can also be given
in named form, in which case the order in which they appear is irrelevant.

The first two parameters may be named from=value and to=value; thus
seq(1,30) and seq(from=1,to=30) and seq(to=30,from=1) are all
the same as 1:30

Ms. Beryl Ang’iro August 21, 2020 9 / 12


Simple Manipulations
Generating regular sequences

The seq() function

This is a more general function for generating sequences. It has five


arguments, only some of which may be specified in any one call.

The first two arguments specify the beginning and end of the sequence,
and if these are the only two arguments given, the result is the same as
the colon operator i.e seq(2,10) is the same vector as 2:10.

The parameters to seq(), and to many other functions can also be given
in named form, in which case the order in which they appear is irrelevant.

The first two parameters may be named from=value and to=value; thus
seq(1,30) and seq(from=1,to=30) and seq(to=30,from=1) are all
the same as 1:30

Ms. Beryl Ang’iro August 21, 2020 9 / 12


Simple Manipulations
Generating regular sequences

The next two parameters to seq() may be named by=value and


length=value which specify a step size and a length for the sequence
respectively. If neither of these is given, the default by=1 is used.
For example,
> s <- seq(-5,5, by=0.2)
generates in s the vector c(-5.0,-4.8,-4.6,...,4.6,4.8,5.0).
Similarly
> s2 <- seq(length=51,from=-5, by=0.2)
generates the same vector in s2 as in s.
A related function is rep() which can be used for replicating an object in
various ways. The simplest form is;
> s3 <- rep(x, times=5)
which will put five copies of x end-to-end in s3.

Ms. Beryl Ang’iro August 21, 2020 10 / 12


Simple Manipulations
Generating regular sequences

The next two parameters to seq() may be named by=value and


length=value which specify a step size and a length for the sequence
respectively. If neither of these is given, the default by=1 is used.
For example,
> s <- seq(-5,5, by=0.2)
generates in s the vector c(-5.0,-4.8,-4.6,...,4.6,4.8,5.0).
Similarly
> s2 <- seq(length=51,from=-5, by=0.2)
generates the same vector in s2 as in s.
A related function is rep() which can be used for replicating an object in
various ways. The simplest form is;
> s3 <- rep(x, times=5)
which will put five copies of x end-to-end in s3.

Ms. Beryl Ang’iro August 21, 2020 10 / 12


Simple Manipulations
Generating regular sequences

The next two parameters to seq() may be named by=value and


length=value which specify a step size and a length for the sequence
respectively. If neither of these is given, the default by=1 is used.
For example,
> s <- seq(-5,5, by=0.2)
generates in s the vector c(-5.0,-4.8,-4.6,...,4.6,4.8,5.0).
Similarly
> s2 <- seq(length=51,from=-5, by=0.2)
generates the same vector in s2 as in s.
A related function is rep() which can be used for replicating an object in
various ways. The simplest form is;
> s3 <- rep(x, times=5)
which will put five copies of x end-to-end in s3.

Ms. Beryl Ang’iro August 21, 2020 10 / 12


Simple Manipulations
Generating regular sequences

The next two parameters to seq() may be named by=value and


length=value which specify a step size and a length for the sequence
respectively. If neither of these is given, the default by=1 is used.
For example,
> s <- seq(-5,5, by=0.2)
generates in s the vector c(-5.0,-4.8,-4.6,...,4.6,4.8,5.0).
Similarly
> s2 <- seq(length=51,from=-5, by=0.2)
generates the same vector in s2 as in s.
A related function is rep() which can be used for replicating an object in
various ways. The simplest form is;
> s3 <- rep(x, times=5)
which will put five copies of x end-to-end in s3.

Ms. Beryl Ang’iro August 21, 2020 10 / 12


Simple Manipulations
Generating regular sequences

The next two parameters to seq() may be named by=value and


length=value which specify a step size and a length for the sequence
respectively. If neither of these is given, the default by=1 is used.
For example,
> s <- seq(-5,5, by=0.2)
generates in s the vector c(-5.0,-4.8,-4.6,...,4.6,4.8,5.0).
Similarly
> s2 <- seq(length=51,from=-5, by=0.2)
generates the same vector in s2 as in s.
A related function is rep() which can be used for replicating an object in
various ways. The simplest form is;
> s3 <- rep(x, times=5)
which will put five copies of x end-to-end in s3.

Ms. Beryl Ang’iro August 21, 2020 10 / 12


Simple Manipulations
Generating regular sequences

The next two parameters to seq() may be named by=value and


length=value which specify a step size and a length for the sequence
respectively. If neither of these is given, the default by=1 is used.
For example,
> s <- seq(-5,5, by=0.2)
generates in s the vector c(-5.0,-4.8,-4.6,...,4.6,4.8,5.0).
Similarly
> s2 <- seq(length=51,from=-5, by=0.2)
generates the same vector in s2 as in s.
A related function is rep() which can be used for replicating an object in
various ways. The simplest form is;
> s3 <- rep(x, times=5)
which will put five copies of x end-to-end in s3.

Ms. Beryl Ang’iro August 21, 2020 10 / 12


Simple Manipulations
Generating regular sequences

The next two parameters to seq() may be named by=value and


length=value which specify a step size and a length for the sequence
respectively. If neither of these is given, the default by=1 is used.
For example,
> s <- seq(-5,5, by=0.2)
generates in s the vector c(-5.0,-4.8,-4.6,...,4.6,4.8,5.0).
Similarly
> s2 <- seq(length=51,from=-5, by=0.2)
generates the same vector in s2 as in s.
A related function is rep() which can be used for replicating an object in
various ways. The simplest form is;
> s3 <- rep(x, times=5)
which will put five copies of x end-to-end in s3.

Ms. Beryl Ang’iro August 21, 2020 10 / 12


Simple Manipulations
Logical vectors

Logical vectors

As well as numerical vectors, R allows manipulation of logical quantities.


The elements of logical vectors have just two possible values, represented
formally as FALSE and TRUE. These are usually abbreviated as F and T
respectively.

Logical operators are generated by conditions. The logical operators are


<, <=, >, >=, == for exact equality, and ! = for inequality.

For example
> temp <- x>13
[1] FALSE FALSE FALSE FALSE TRUE
In addition if a and b are logical expressions, then ab is their intersection
(”and”), a|b is their union (”or”) and !a is the negation of a.

Ms. Beryl Ang’iro August 21, 2020 11 / 12


Simple Manipulations
Logical vectors

Logical vectors

As well as numerical vectors, R allows manipulation of logical quantities.


The elements of logical vectors have just two possible values, represented
formally as FALSE and TRUE. These are usually abbreviated as F and T
respectively.

Logical operators are generated by conditions. The logical operators are


<, <=, >, >=, == for exact equality, and ! = for inequality.

For example
> temp <- x>13
[1] FALSE FALSE FALSE FALSE TRUE
In addition if a and b are logical expressions, then ab is their intersection
(”and”), a|b is their union (”or”) and !a is the negation of a.

Ms. Beryl Ang’iro August 21, 2020 11 / 12


Simple Manipulations
Logical vectors

Logical vectors

As well as numerical vectors, R allows manipulation of logical quantities.


The elements of logical vectors have just two possible values, represented
formally as FALSE and TRUE. These are usually abbreviated as F and T
respectively.

Logical operators are generated by conditions. The logical operators are


<, <=, >, >=, == for exact equality, and ! = for inequality.

For example
> temp <- x>13
[1] FALSE FALSE FALSE FALSE TRUE
In addition if a and b are logical expressions, then ab is their intersection
(”and”), a|b is their union (”or”) and !a is the negation of a.

Ms. Beryl Ang’iro August 21, 2020 11 / 12


Simple Manipulations
Logical vectors

Logical vectors

As well as numerical vectors, R allows manipulation of logical quantities.


The elements of logical vectors have just two possible values, represented
formally as FALSE and TRUE. These are usually abbreviated as F and T
respectively.

Logical operators are generated by conditions. The logical operators are


<, <=, >, >=, == for exact equality, and ! = for inequality.

For example
> temp <- x>13
[1] FALSE FALSE FALSE FALSE TRUE
In addition if a and b are logical expressions, then ab is their intersection
(”and”), a|b is their union (”or”) and !a is the negation of a.

Ms. Beryl Ang’iro August 21, 2020 11 / 12


Simple Manipulations
Logical vectors

Logical vectors

As well as numerical vectors, R allows manipulation of logical quantities.


The elements of logical vectors have just two possible values, represented
formally as FALSE and TRUE. These are usually abbreviated as F and T
respectively.

Logical operators are generated by conditions. The logical operators are


<, <=, >, >=, == for exact equality, and ! = for inequality.

For example
> temp <- x>13
[1] FALSE FALSE FALSE FALSE TRUE
In addition if a and b are logical expressions, then ab is their intersection
(”and”), a|b is their union (”or”) and !a is the negation of a.

Ms. Beryl Ang’iro August 21, 2020 11 / 12


Simple Manipulations
Logical vectors

For example,
> w <- x>10
>w
[1] TRUE FALSE FALSE FALSE TRUE
> z <- x<=10
>z
[1] FALSE TRUE TRUE TRUE FALSE
The intersection of w and z is;
> w&z
[1] FALSE FALSE FALSE FALSE FALSE
The union of w and z is;
> w|z
[1] TRUE TRUE TRUE TRUE TRUE
The negation of w
> !w
[1] FALSE TRUE TRUE TRUE FALSE
Ms. Beryl Ang’iro August 21, 2020 12 / 12
Simple Manipulations
Logical vectors

For example,
> w <- x>10
>w
[1] TRUE FALSE FALSE FALSE TRUE
> z <- x<=10
>z
[1] FALSE TRUE TRUE TRUE FALSE
The intersection of w and z is;
> w&z
[1] FALSE FALSE FALSE FALSE FALSE
The union of w and z is;
> w|z
[1] TRUE TRUE TRUE TRUE TRUE
The negation of w
> !w
[1] FALSE TRUE TRUE TRUE FALSE
Ms. Beryl Ang’iro August 21, 2020 12 / 12
Simple Manipulations
Logical vectors

For example,
> w <- x>10
>w
[1] TRUE FALSE FALSE FALSE TRUE
> z <- x<=10
>z
[1] FALSE TRUE TRUE TRUE FALSE
The intersection of w and z is;
> w&z
[1] FALSE FALSE FALSE FALSE FALSE
The union of w and z is;
> w|z
[1] TRUE TRUE TRUE TRUE TRUE
The negation of w
> !w
[1] FALSE TRUE TRUE TRUE FALSE
Ms. Beryl Ang’iro August 21, 2020 12 / 12
Simple Manipulations
Logical vectors

For example,
> w <- x>10
>w
[1] TRUE FALSE FALSE FALSE TRUE
> z <- x<=10
>z
[1] FALSE TRUE TRUE TRUE FALSE
The intersection of w and z is;
> w&z
[1] FALSE FALSE FALSE FALSE FALSE
The union of w and z is;
> w|z
[1] TRUE TRUE TRUE TRUE TRUE
The negation of w
> !w
[1] FALSE TRUE TRUE TRUE FALSE
Ms. Beryl Ang’iro August 21, 2020 12 / 12
Simple Manipulations
Logical vectors

For example,
> w <- x>10
>w
[1] TRUE FALSE FALSE FALSE TRUE
> z <- x<=10
>z
[1] FALSE TRUE TRUE TRUE FALSE
The intersection of w and z is;
> w&z
[1] FALSE FALSE FALSE FALSE FALSE
The union of w and z is;
> w|z
[1] TRUE TRUE TRUE TRUE TRUE
The negation of w
> !w
[1] FALSE TRUE TRUE TRUE FALSE
Ms. Beryl Ang’iro August 21, 2020 12 / 12
Simple Manipulations
Logical vectors

For example,
> w <- x>10
>w
[1] TRUE FALSE FALSE FALSE TRUE
> z <- x<=10
>z
[1] FALSE TRUE TRUE TRUE FALSE
The intersection of w and z is;
> w&z
[1] FALSE FALSE FALSE FALSE FALSE
The union of w and z is;
> w|z
[1] TRUE TRUE TRUE TRUE TRUE
The negation of w
> !w
[1] FALSE TRUE TRUE TRUE FALSE
Ms. Beryl Ang’iro August 21, 2020 12 / 12
Simple Manipulations
Logical vectors

For example,
> w <- x>10
>w
[1] TRUE FALSE FALSE FALSE TRUE
> z <- x<=10
>z
[1] FALSE TRUE TRUE TRUE FALSE
The intersection of w and z is;
> w&z
[1] FALSE FALSE FALSE FALSE FALSE
The union of w and z is;
> w|z
[1] TRUE TRUE TRUE TRUE TRUE
The negation of w
> !w
[1] FALSE TRUE TRUE TRUE FALSE
Ms. Beryl Ang’iro August 21, 2020 12 / 12
Simple Manipulations
Logical vectors

For example,
> w <- x>10
>w
[1] TRUE FALSE FALSE FALSE TRUE
> z <- x<=10
>z
[1] FALSE TRUE TRUE TRUE FALSE
The intersection of w and z is;
> w&z
[1] FALSE FALSE FALSE FALSE FALSE
The union of w and z is;
> w|z
[1] TRUE TRUE TRUE TRUE TRUE
The negation of w
> !w
[1] FALSE TRUE TRUE TRUE FALSE
Ms. Beryl Ang’iro August 21, 2020 12 / 12
Simple Manipulations
Logical vectors

For example,
> w <- x>10
>w
[1] TRUE FALSE FALSE FALSE TRUE
> z <- x<=10
>z
[1] FALSE TRUE TRUE TRUE FALSE
The intersection of w and z is;
> w&z
[1] FALSE FALSE FALSE FALSE FALSE
The union of w and z is;
> w|z
[1] TRUE TRUE TRUE TRUE TRUE
The negation of w
> !w
[1] FALSE TRUE TRUE TRUE FALSE
Ms. Beryl Ang’iro August 21, 2020 12 / 12
Simple Manipulations
Logical vectors

For example,
> w <- x>10
>w
[1] TRUE FALSE FALSE FALSE TRUE
> z <- x<=10
>z
[1] FALSE TRUE TRUE TRUE FALSE
The intersection of w and z is;
> w&z
[1] FALSE FALSE FALSE FALSE FALSE
The union of w and z is;
> w|z
[1] TRUE TRUE TRUE TRUE TRUE
The negation of w
> !w
[1] FALSE TRUE TRUE TRUE FALSE
Ms. Beryl Ang’iro August 21, 2020 12 / 12

You might also like