R - Lecture - Notes (Lecture 3) PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 86

Lecture 3: Statistical Computing

Simple Manipulations: Numbers and Vectors in RStudio

Ms. Beryl Ang’iro

September 3, 2020

Ms. Beryl Ang’iro September 3, 2020 1 / 19


Getting Started with RStudio
What is RStudio

What is RStudio

RStudio is an integrated development environment (IDE) for R that


provides an alternative interface to R that has several advantages over the
default R interfaces;
• RStudio can run in a web browser. In addition to stand-alone
desktop versions, RStudio can be set up as a server application that
is accessed via the internet.
• RStudio provides support for reproducible research. It makes it easy
to include text, statistical analysis (R code and R output), and
graphical displays all in the same document. The RMarkdown system
provides a simple markup language and renders the results in HTML.
• RStudio provides an integrated support for editing and executing R
code and documents.

Ms. Beryl Ang’iro September 3, 2020 2 / 19


Getting Started with RStudio
What is RStudio

What is RStudio

RStudio is an integrated development environment (IDE) for R that


provides an alternative interface to R that has several advantages over the
default R interfaces;
• RStudio can run in a web browser. In addition to stand-alone
desktop versions, RStudio can be set up as a server application that
is accessed via the internet.
• RStudio provides support for reproducible research. It makes it easy
to include text, statistical analysis (R code and R output), and
graphical displays all in the same document. The RMarkdown system
provides a simple markup language and renders the results in HTML.
• RStudio provides an integrated support for editing and executing R
code and documents.

Ms. Beryl Ang’iro September 3, 2020 2 / 19


Getting Started with RStudio
What is RStudio

What is RStudio

RStudio is an integrated development environment (IDE) for R that


provides an alternative interface to R that has several advantages over the
default R interfaces;
• RStudio can run in a web browser. In addition to stand-alone
desktop versions, RStudio can be set up as a server application that
is accessed via the internet.
• RStudio provides support for reproducible research. It makes it easy
to include text, statistical analysis (R code and R output), and
graphical displays all in the same document. The RMarkdown system
provides a simple markup language and renders the results in HTML.
• RStudio provides an integrated support for editing and executing R
code and documents.

Ms. Beryl Ang’iro September 3, 2020 2 / 19


Getting Started with RStudio
What is RStudio

What is RStudio

RStudio is an integrated development environment (IDE) for R that


provides an alternative interface to R that has several advantages over the
default R interfaces;
• RStudio can run in a web browser. In addition to stand-alone
desktop versions, RStudio can be set up as a server application that
is accessed via the internet.
• RStudio provides support for reproducible research. It makes it easy
to include text, statistical analysis (R code and R output), and
graphical displays all in the same document. The RMarkdown system
provides a simple markup language and renders the results in HTML.
• RStudio provides an integrated support for editing and executing R
code and documents.

Ms. Beryl Ang’iro September 3, 2020 2 / 19


Getting Started with RStudio
What is RStudio

• RStudio provides some useful functionality via a graphical user


interface. RStudio is not a GUI (Graphical User Interface) for R, but
it does provide a GUI that simplifies things like installing and
updating packages; monitoring, saving and loading environments;
importing and exporting data; browsing and exporting graphics; and
browsing files and documentation.
While one can certainly use R without using RStudio, RStudio makes a
number of things easier and is highly recommended.
RStudio is available from

http://www.rstudio.org/

RStudio divides its world into four panels. Several of the panels are
further subdivided into multiple tabs.

Ms. Beryl Ang’iro September 3, 2020 3 / 19


Getting Started with RStudio
What is RStudio

• RStudio provides some useful functionality via a graphical user


interface. RStudio is not a GUI (Graphical User Interface) for R, but
it does provide a GUI that simplifies things like installing and
updating packages; monitoring, saving and loading environments;
importing and exporting data; browsing and exporting graphics; and
browsing files and documentation.
While one can certainly use R without using RStudio, RStudio makes a
number of things easier and is highly recommended.
RStudio is available from

http://www.rstudio.org/

RStudio divides its world into four panels. Several of the panels are
further subdivided into multiple tabs.

Ms. Beryl Ang’iro September 3, 2020 3 / 19


Getting Started with RStudio
What is RStudio

• RStudio provides some useful functionality via a graphical user


interface. RStudio is not a GUI (Graphical User Interface) for R, but
it does provide a GUI that simplifies things like installing and
updating packages; monitoring, saving and loading environments;
importing and exporting data; browsing and exporting graphics; and
browsing files and documentation.
While one can certainly use R without using RStudio, RStudio makes a
number of things easier and is highly recommended.
RStudio is available from

http://www.rstudio.org/

RStudio divides its world into four panels. Several of the panels are
further subdivided into multiple tabs.

Ms. Beryl Ang’iro September 3, 2020 3 / 19


Getting Started with RStudio
What is RStudio

• RStudio provides some useful functionality via a graphical user


interface. RStudio is not a GUI (Graphical User Interface) for R, but
it does provide a GUI that simplifies things like installing and
updating packages; monitoring, saving and loading environments;
importing and exporting data; browsing and exporting graphics; and
browsing files and documentation.
While one can certainly use R without using RStudio, RStudio makes a
number of things easier and is highly recommended.
RStudio is available from

http://www.rstudio.org/

RStudio divides its world into four panels. Several of the panels are
further subdivided into multiple tabs.

Ms. Beryl Ang’iro September 3, 2020 3 / 19


Getting Started with RStudio
What is RStudio

Ms. Beryl Ang’iro September 3, 2020 4 / 19


Getting Started with RStudio
What is RStudio

Console: This is where results are displayed. The console tab is opened
when RStudio is opened. All interactions with commands, i.e where
commands are run.
Source editor: Where commands and scripts are typed in and saved for
later use.
Workspace Environment: Displays data objects defined in the current R
session. i.e where objects created are stored.
History: Is a list of prior commands which have been executed. Keeps
commands you have used.
File browser: is a folder browser which shows you where you are.
Plots: Displays plots you create.
Packages: Bundle of functions where packages can be installed and
loaded from.
Help: Where help on R commands can be found
Viewer: Where web content can be viewed.
Ms. Beryl Ang’iro September 3, 2020 5 / 19
Getting Started with RStudio
What is RStudio

Console: This is where results are displayed. The console tab is opened
when RStudio is opened. All interactions with commands, i.e where
commands are run.
Source editor: Where commands and scripts are typed in and saved for
later use.
Workspace Environment: Displays data objects defined in the current R
session. i.e where objects created are stored.
History: Is a list of prior commands which have been executed. Keeps
commands you have used.
File browser: is a folder browser which shows you where you are.
Plots: Displays plots you create.
Packages: Bundle of functions where packages can be installed and
loaded from.
Help: Where help on R commands can be found
Viewer: Where web content can be viewed.
Ms. Beryl Ang’iro September 3, 2020 5 / 19
Getting Started with RStudio
What is RStudio

Console: This is where results are displayed. The console tab is opened
when RStudio is opened. All interactions with commands, i.e where
commands are run.
Source editor: Where commands and scripts are typed in and saved for
later use.
Workspace Environment: Displays data objects defined in the current R
session. i.e where objects created are stored.
History: Is a list of prior commands which have been executed. Keeps
commands you have used.
File browser: is a folder browser which shows you where you are.
Plots: Displays plots you create.
Packages: Bundle of functions where packages can be installed and
loaded from.
Help: Where help on R commands can be found
Viewer: Where web content can be viewed.
Ms. Beryl Ang’iro September 3, 2020 5 / 19
Getting Started with RStudio
What is RStudio

Console: This is where results are displayed. The console tab is opened
when RStudio is opened. All interactions with commands, i.e where
commands are run.
Source editor: Where commands and scripts are typed in and saved for
later use.
Workspace Environment: Displays data objects defined in the current R
session. i.e where objects created are stored.
History: Is a list of prior commands which have been executed. Keeps
commands you have used.
File browser: is a folder browser which shows you where you are.
Plots: Displays plots you create.
Packages: Bundle of functions where packages can be installed and
loaded from.
Help: Where help on R commands can be found
Viewer: Where web content can be viewed.
Ms. Beryl Ang’iro September 3, 2020 5 / 19
Getting Started with RStudio
What is RStudio

Console: This is where results are displayed. The console tab is opened
when RStudio is opened. All interactions with commands, i.e where
commands are run.
Source editor: Where commands and scripts are typed in and saved for
later use.
Workspace Environment: Displays data objects defined in the current R
session. i.e where objects created are stored.
History: Is a list of prior commands which have been executed. Keeps
commands you have used.
File browser: is a folder browser which shows you where you are.
Plots: Displays plots you create.
Packages: Bundle of functions where packages can be installed and
loaded from.
Help: Where help on R commands can be found
Viewer: Where web content can be viewed.
Ms. Beryl Ang’iro September 3, 2020 5 / 19
Getting Started with RStudio
What is RStudio

Console: This is where results are displayed. The console tab is opened
when RStudio is opened. All interactions with commands, i.e where
commands are run.
Source editor: Where commands and scripts are typed in and saved for
later use.
Workspace Environment: Displays data objects defined in the current R
session. i.e where objects created are stored.
History: Is a list of prior commands which have been executed. Keeps
commands you have used.
File browser: is a folder browser which shows you where you are.
Plots: Displays plots you create.
Packages: Bundle of functions where packages can be installed and
loaded from.
Help: Where help on R commands can be found
Viewer: Where web content can be viewed.
Ms. Beryl Ang’iro September 3, 2020 5 / 19
Getting Started with RStudio
What is RStudio

Console: This is where results are displayed. The console tab is opened
when RStudio is opened. All interactions with commands, i.e where
commands are run.
Source editor: Where commands and scripts are typed in and saved for
later use.
Workspace Environment: Displays data objects defined in the current R
session. i.e where objects created are stored.
History: Is a list of prior commands which have been executed. Keeps
commands you have used.
File browser: is a folder browser which shows you where you are.
Plots: Displays plots you create.
Packages: Bundle of functions where packages can be installed and
loaded from.
Help: Where help on R commands can be found
Viewer: Where web content can be viewed.
Ms. Beryl Ang’iro September 3, 2020 5 / 19
Getting Started with RStudio
What is RStudio

Console: This is where results are displayed. The console tab is opened
when RStudio is opened. All interactions with commands, i.e where
commands are run.
Source editor: Where commands and scripts are typed in and saved for
later use.
Workspace Environment: Displays data objects defined in the current R
session. i.e where objects created are stored.
History: Is a list of prior commands which have been executed. Keeps
commands you have used.
File browser: is a folder browser which shows you where you are.
Plots: Displays plots you create.
Packages: Bundle of functions where packages can be installed and
loaded from.
Help: Where help on R commands can be found
Viewer: Where web content can be viewed.
Ms. Beryl Ang’iro September 3, 2020 5 / 19
Getting Started with RStudio
What is RStudio

Console: This is where results are displayed. The console tab is opened
when RStudio is opened. All interactions with commands, i.e where
commands are run.
Source editor: Where commands and scripts are typed in and saved for
later use.
Workspace Environment: Displays data objects defined in the current R
session. i.e where objects created are stored.
History: Is a list of prior commands which have been executed. Keeps
commands you have used.
File browser: is a folder browser which shows you where you are.
Plots: Displays plots you create.
Packages: Bundle of functions where packages can be installed and
loaded from.
Help: Where help on R commands can be found
Viewer: Where web content can be viewed.
Ms. Beryl Ang’iro September 3, 2020 5 / 19
Simple manipulations continuation
Missing values

Missing values

In some cases the components of a vector may not be completely known.


When an element or value is ”not available” in the statistical sense, a place
within a vector may be reserved for it by assigning it the special value NA.

NA is a recognized element in R e.g,


> x <- c(3,1,NA)
>x
[1] 3 1 NA
The function is.na(x) gives a logical vector of the same size as x with
value TRUE if and only if the corresponding element in x is NA.
> is.na(x)
[1] FALSE FALSE TRUE

Ms. Beryl Ang’iro September 3, 2020 6 / 19


Simple manipulations continuation
Missing values

Missing values

In some cases the components of a vector may not be completely known.


When an element or value is ”not available” in the statistical sense, a place
within a vector may be reserved for it by assigning it the special value NA.

NA is a recognized element in R e.g,


> x <- c(3,1,NA)
>x
[1] 3 1 NA
The function is.na(x) gives a logical vector of the same size as x with
value TRUE if and only if the corresponding element in x is NA.
> is.na(x)
[1] FALSE FALSE TRUE

Ms. Beryl Ang’iro September 3, 2020 6 / 19


Simple manipulations continuation
Missing values

Missing values

In some cases the components of a vector may not be completely known.


When an element or value is ”not available” in the statistical sense, a place
within a vector may be reserved for it by assigning it the special value NA.

NA is a recognized element in R e.g,


> x <- c(3,1,NA)
>x
[1] 3 1 NA
The function is.na(x) gives a logical vector of the same size as x with
value TRUE if and only if the corresponding element in x is NA.
> is.na(x)
[1] FALSE FALSE TRUE

Ms. Beryl Ang’iro September 3, 2020 6 / 19


Simple manipulations continuation
Missing values

Missing values

In some cases the components of a vector may not be completely known.


When an element or value is ”not available” in the statistical sense, a place
within a vector may be reserved for it by assigning it the special value NA.

NA is a recognized element in R e.g,


> x <- c(3,1,NA)
>x
[1] 3 1 NA
The function is.na(x) gives a logical vector of the same size as x with
value TRUE if and only if the corresponding element in x is NA.
> is.na(x)
[1] FALSE FALSE TRUE

Ms. Beryl Ang’iro September 3, 2020 6 / 19


Simple manipulations continuation
Missing values

> x==NA
[1] NA NA NA
Notice that the logical expression x==NA is quite different from is.na(x)
since NA is not really a value but a marker for a quantity that is not
available.
Thus x==NA is a vector of the same length as x all of whose values are NA
as the logical expression itself is incomplete and hence undecidable.
Functions have special actions when they encounter values of NA, and may
have arguments to control the handling of NAs e.g
> mean(x)
[1] NA
In general, any operation on an NA becomes an NA. The motivation for this
rule is simply that if the specification of an operation is incomplete, the
results cannot be known and hence is not available.
Ms. Beryl Ang’iro September 3, 2020 7 / 19
Simple manipulations continuation
Missing values

> x==NA
[1] NA NA NA
Notice that the logical expression x==NA is quite different from is.na(x)
since NA is not really a value but a marker for a quantity that is not
available.
Thus x==NA is a vector of the same length as x all of whose values are NA
as the logical expression itself is incomplete and hence undecidable.
Functions have special actions when they encounter values of NA, and may
have arguments to control the handling of NAs e.g
> mean(x)
[1] NA
In general, any operation on an NA becomes an NA. The motivation for this
rule is simply that if the specification of an operation is incomplete, the
results cannot be known and hence is not available.
Ms. Beryl Ang’iro September 3, 2020 7 / 19
Simple manipulations continuation
Missing values

> x==NA
[1] NA NA NA
Notice that the logical expression x==NA is quite different from is.na(x)
since NA is not really a value but a marker for a quantity that is not
available.
Thus x==NA is a vector of the same length as x all of whose values are NA
as the logical expression itself is incomplete and hence undecidable.
Functions have special actions when they encounter values of NA, and may
have arguments to control the handling of NAs e.g
> mean(x)
[1] NA
In general, any operation on an NA becomes an NA. The motivation for this
rule is simply that if the specification of an operation is incomplete, the
results cannot be known and hence is not available.
Ms. Beryl Ang’iro September 3, 2020 7 / 19
Simple manipulations continuation
Missing values

> x==NA
[1] NA NA NA
Notice that the logical expression x==NA is quite different from is.na(x)
since NA is not really a value but a marker for a quantity that is not
available.
Thus x==NA is a vector of the same length as x all of whose values are NA
as the logical expression itself is incomplete and hence undecidable.
Functions have special actions when they encounter values of NA, and may
have arguments to control the handling of NAs e.g
> mean(x)
[1] NA
In general, any operation on an NA becomes an NA. The motivation for this
rule is simply that if the specification of an operation is incomplete, the
results cannot be known and hence is not available.
Ms. Beryl Ang’iro September 3, 2020 7 / 19
Simple manipulations continuation
Missing values

> x==NA
[1] NA NA NA
Notice that the logical expression x==NA is quite different from is.na(x)
since NA is not really a value but a marker for a quantity that is not
available.
Thus x==NA is a vector of the same length as x all of whose values are NA
as the logical expression itself is incomplete and hence undecidable.
Functions have special actions when they encounter values of NA, and may
have arguments to control the handling of NAs e.g
> mean(x)
[1] NA
In general, any operation on an NA becomes an NA. The motivation for this
rule is simply that if the specification of an operation is incomplete, the
results cannot be known and hence is not available.
Ms. Beryl Ang’iro September 3, 2020 7 / 19
Simple manipulations continuation
Missing values

Note that there is a second kind of ”missing” values which are produced
by numerical computation, the so-called Not a Number, NaN values.

For example;
> 0/0
[1] NaN

or
> Inf-Inf
[1] NaN

These both gives NaN since the results cannot be defined sensibly. In
summary, is.na(x) is TRUE both for NA and NaN values.

Ms. Beryl Ang’iro September 3, 2020 8 / 19


Simple manipulations continuation
Missing values

Note that there is a second kind of ”missing” values which are produced
by numerical computation, the so-called Not a Number, NaN values.

For example;
> 0/0
[1] NaN

or
> Inf-Inf
[1] NaN

These both gives NaN since the results cannot be defined sensibly. In
summary, is.na(x) is TRUE both for NA and NaN values.

Ms. Beryl Ang’iro September 3, 2020 8 / 19


Simple manipulations continuation
Missing values

Note that there is a second kind of ”missing” values which are produced
by numerical computation, the so-called Not a Number, NaN values.

For example;
> 0/0
[1] NaN

or
> Inf-Inf
[1] NaN

These both gives NaN since the results cannot be defined sensibly. In
summary, is.na(x) is TRUE both for NA and NaN values.

Ms. Beryl Ang’iro September 3, 2020 8 / 19


Simple manipulations continuation
Missing values

Note that there is a second kind of ”missing” values which are produced
by numerical computation, the so-called Not a Number, NaN values.

For example;
> 0/0
[1] NaN

or
> Inf-Inf
[1] NaN

These both gives NaN since the results cannot be defined sensibly. In
summary, is.na(x) is TRUE both for NA and NaN values.

Ms. Beryl Ang’iro September 3, 2020 8 / 19


Simple manipulations continuation
Character vectors

Character vectors
Text in R is represented by character vectors. A character vector is a
vector consisting of characters.

Where needed they are denoted by a sequence of characters by the double


quote character e.g;

> y <- "Hello"


>y
[1] "Hello"

Character vectors may be concatenated into a vector by the c() function.


e.g
> z <- c("a", "bc", "def")
[1] "a" "bc" "def"
Ms. Beryl Ang’iro September 3, 2020 9 / 19
Simple manipulations continuation
Character vectors

Character vectors
Text in R is represented by character vectors. A character vector is a
vector consisting of characters.

Where needed they are denoted by a sequence of characters by the double


quote character e.g;

> y <- "Hello"


>y
[1] "Hello"

Character vectors may be concatenated into a vector by the c() function.


e.g
> z <- c("a", "bc", "def")
[1] "a" "bc" "def"
Ms. Beryl Ang’iro September 3, 2020 9 / 19
Simple manipulations continuation
Character vectors

Character vectors
Text in R is represented by character vectors. A character vector is a
vector consisting of characters.

Where needed they are denoted by a sequence of characters by the double


quote character e.g;

> y <- "Hello"


>y
[1] "Hello"

Character vectors may be concatenated into a vector by the c() function.


e.g
> z <- c("a", "bc", "def")
[1] "a" "bc" "def"
Ms. Beryl Ang’iro September 3, 2020 9 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

Index vectors, selecting and modifying subsets of a dataset


The indexing system is an efficient and flexible way to access selectively
the elements of an object; it can be either numeric or logical.

Subsets of elements of a vector may be selected by appending to the name


of the vector an index vector in square brackets ([ ]). The expression must
be of the form vector[index vector].
More generally, any expression that evaluates to a vector may have subsets
of its elements similarly selected by appending an index vector in square
brackets immediately after the expression.
To access, for example, the third value of a vector x, we just type x[3]
which can be used either to extract or to change this value;
> x <- 5:10
x
[1] 5 6 7 8 9 10
Ms. Beryl Ang’iro September 3, 2020 10 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

Index vectors, selecting and modifying subsets of a dataset


The indexing system is an efficient and flexible way to access selectively
the elements of an object; it can be either numeric or logical.

Subsets of elements of a vector may be selected by appending to the name


of the vector an index vector in square brackets ([ ]). The expression must
be of the form vector[index vector].
More generally, any expression that evaluates to a vector may have subsets
of its elements similarly selected by appending an index vector in square
brackets immediately after the expression.
To access, for example, the third value of a vector x, we just type x[3]
which can be used either to extract or to change this value;
> x <- 5:10
x
[1] 5 6 7 8 9 10
Ms. Beryl Ang’iro September 3, 2020 10 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

Index vectors, selecting and modifying subsets of a dataset


The indexing system is an efficient and flexible way to access selectively
the elements of an object; it can be either numeric or logical.

Subsets of elements of a vector may be selected by appending to the name


of the vector an index vector in square brackets ([ ]). The expression must
be of the form vector[index vector].
More generally, any expression that evaluates to a vector may have subsets
of its elements similarly selected by appending an index vector in square
brackets immediately after the expression.
To access, for example, the third value of a vector x, we just type x[3]
which can be used either to extract or to change this value;
> x <- 5:10
x
[1] 5 6 7 8 9 10
Ms. Beryl Ang’iro September 3, 2020 10 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

Index vectors, selecting and modifying subsets of a dataset


The indexing system is an efficient and flexible way to access selectively
the elements of an object; it can be either numeric or logical.

Subsets of elements of a vector may be selected by appending to the name


of the vector an index vector in square brackets ([ ]). The expression must
be of the form vector[index vector].
More generally, any expression that evaluates to a vector may have subsets
of its elements similarly selected by appending an index vector in square
brackets immediately after the expression.
To access, for example, the third value of a vector x, we just type x[3]
which can be used either to extract or to change this value;
> x <- 5:10
x
[1] 5 6 7 8 9 10
Ms. Beryl Ang’iro September 3, 2020 10 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

> x[3] ##Extracts the third element

[1] 7

> x[3] <- 20 ##Changes value of 3rd element from 7 to 20

[1] 5 6 20 8 9 10

Such index vectors can be any of the four distinct types;


• A logical vector.
• A vector of positive integral quantities.
• A vector of negative integral quantities.
• A vector of character strings.

Ms. Beryl Ang’iro September 3, 2020 11 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

> x[3] ##Extracts the third element

[1] 7

> x[3] <- 20 ##Changes value of 3rd element from 7 to 20

[1] 5 6 20 8 9 10

Such index vectors can be any of the four distinct types;


• A logical vector.
• A vector of positive integral quantities.
• A vector of negative integral quantities.
• A vector of character strings.

Ms. Beryl Ang’iro September 3, 2020 11 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

> x[3] ##Extracts the third element

[1] 7

> x[3] <- 20 ##Changes value of 3rd element from 7 to 20

[1] 5 6 20 8 9 10

Such index vectors can be any of the four distinct types;


• A logical vector.
• A vector of positive integral quantities.
• A vector of negative integral quantities.
• A vector of character strings.

Ms. Beryl Ang’iro September 3, 2020 11 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

> x[3] ##Extracts the third element

[1] 7

> x[3] <- 20 ##Changes value of 3rd element from 7 to 20

[1] 5 6 20 8 9 10

Such index vectors can be any of the four distinct types;


• A logical vector.
• A vector of positive integral quantities.
• A vector of negative integral quantities.
• A vector of character strings.

Ms. Beryl Ang’iro September 3, 2020 11 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

> x[3] ##Extracts the third element

[1] 7

> x[3] <- 20 ##Changes value of 3rd element from 7 to 20

[1] 5 6 20 8 9 10

Such index vectors can be any of the four distinct types;


• A logical vector.
• A vector of positive integral quantities.
• A vector of negative integral quantities.
• A vector of character strings.

Ms. Beryl Ang’iro September 3, 2020 11 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

> x[3] ##Extracts the third element

[1] 7

> x[3] <- 20 ##Changes value of 3rd element from 7 to 20

[1] 5 6 20 8 9 10

Such index vectors can be any of the four distinct types;


• A logical vector.
• A vector of positive integral quantities.
• A vector of negative integral quantities.
• A vector of character strings.

Ms. Beryl Ang’iro September 3, 2020 11 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

> x[3] ##Extracts the third element

[1] 7

> x[3] <- 20 ##Changes value of 3rd element from 7 to 20

[1] 5 6 20 8 9 10

Such index vectors can be any of the four distinct types;


• A logical vector.
• A vector of positive integral quantities.
• A vector of negative integral quantities.
• A vector of character strings.

Ms. Beryl Ang’iro September 3, 2020 11 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

> x[3] ##Extracts the third element

[1] 7

> x[3] <- 20 ##Changes value of 3rd element from 7 to 20

[1] 5 6 20 8 9 10

Such index vectors can be any of the four distinct types;


• A logical vector.
• A vector of positive integral quantities.
• A vector of negative integral quantities.
• A vector of character strings.

Ms. Beryl Ang’iro September 3, 2020 11 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A logical vector

In this case, the index must be a vector of the same length as the vector
from which elements are to be selected.

Values corresponding to T in the index vector are selected and those


corresponding to F are omitted.

For example;
> x <- c(-3,10.4,5.6,3.1,6.4,NA,21.7,9.6,2.1,13.3,-1.8,3.4)
> y <- x[!is.na(x)]
>y
[1] -3.0 10.4 5.6 3.1 6.4 21.7 9.6 2.1 13.3 -1.8 3.4
Creates (or recreates) an object y which contains the non-missing values
of x.

Ms. Beryl Ang’iro September 3, 2020 12 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A logical vector

In this case, the index must be a vector of the same length as the vector
from which elements are to be selected.

Values corresponding to T in the index vector are selected and those


corresponding to F are omitted.

For example;
> x <- c(-3,10.4,5.6,3.1,6.4,NA,21.7,9.6,2.1,13.3,-1.8,3.4)
> y <- x[!is.na(x)]
>y
[1] -3.0 10.4 5.6 3.1 6.4 21.7 9.6 2.1 13.3 -1.8 3.4
Creates (or recreates) an object y which contains the non-missing values
of x.

Ms. Beryl Ang’iro September 3, 2020 12 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A logical vector

In this case, the index must be a vector of the same length as the vector
from which elements are to be selected.

Values corresponding to T in the index vector are selected and those


corresponding to F are omitted.

For example;
> x <- c(-3,10.4,5.6,3.1,6.4,NA,21.7,9.6,2.1,13.3,-1.8,3.4)
> y <- x[!is.na(x)]
>y
[1] -3.0 10.4 5.6 3.1 6.4 21.7 9.6 2.1 13.3 -1.8 3.4
Creates (or recreates) an object y which contains the non-missing values
of x.

Ms. Beryl Ang’iro September 3, 2020 12 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A logical vector

In this case, the index must be a vector of the same length as the vector
from which elements are to be selected.

Values corresponding to T in the index vector are selected and those


corresponding to F are omitted.

For example;
> x <- c(-3,10.4,5.6,3.1,6.4,NA,21.7,9.6,2.1,13.3,-1.8,3.4)
> y <- x[!is.na(x)]
>y
[1] -3.0 10.4 5.6 3.1 6.4 21.7 9.6 2.1 13.3 -1.8 3.4
Creates (or recreates) an object y which contains the non-missing values
of x.

Ms. Beryl Ang’iro September 3, 2020 12 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A logical vector

In this case, the index must be a vector of the same length as the vector
from which elements are to be selected.

Values corresponding to T in the index vector are selected and those


corresponding to F are omitted.

For example;
> x <- c(-3,10.4,5.6,3.1,6.4,NA,21.7,9.6,2.1,13.3,-1.8,3.4)
> y <- x[!is.na(x)]
>y
[1] -3.0 10.4 5.6 3.1 6.4 21.7 9.6 2.1 13.3 -1.8 3.4
Creates (or recreates) an object y which contains the non-missing values
of x.

Ms. Beryl Ang’iro September 3, 2020 12 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A logical vector
Also;
> z <- (x+1)[(!is.na(x))&x>0]
>z
[1] 11.4 6.6 4.1 7.4 22.7 10.6 3.1 14.3 4.4
Creates an object z and places it in the values of the vector (x+1) for
which the corresponding value in x was both non-missing and positive.
It is possible to access the values of an element with a comparison
expression as the index.

For example;
> w <- 1:10
> w[w >= 5] <- 20
>w
[1] 1 2 3 4 20 20 20 20 20 20
Ms. Beryl Ang’iro September 3, 2020 13 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A logical vector
Also;
> z <- (x+1)[(!is.na(x))&x>0]
>z
[1] 11.4 6.6 4.1 7.4 22.7 10.6 3.1 14.3 4.4
Creates an object z and places it in the values of the vector (x+1) for
which the corresponding value in x was both non-missing and positive.
It is possible to access the values of an element with a comparison
expression as the index.

For example;
> w <- 1:10
> w[w >= 5] <- 20
>w
[1] 1 2 3 4 20 20 20 20 20 20
Ms. Beryl Ang’iro September 3, 2020 13 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A logical vector
Also;
> z <- (x+1)[(!is.na(x))&x>0]
>z
[1] 11.4 6.6 4.1 7.4 22.7 10.6 3.1 14.3 4.4
Creates an object z and places it in the values of the vector (x+1) for
which the corresponding value in x was both non-missing and positive.
It is possible to access the values of an element with a comparison
expression as the index.

For example;
> w <- 1:10
> w[w >= 5] <- 20
>w
[1] 1 2 3 4 20 20 20 20 20 20
Ms. Beryl Ang’iro September 3, 2020 13 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A logical vector
Also;
> z <- (x+1)[(!is.na(x))&x>0]
>z
[1] 11.4 6.6 4.1 7.4 22.7 10.6 3.1 14.3 4.4
Creates an object z and places it in the values of the vector (x+1) for
which the corresponding value in x was both non-missing and positive.
It is possible to access the values of an element with a comparison
expression as the index.

For example;
> w <- 1:10
> w[w >= 5] <- 20
>w
[1] 1 2 3 4 20 20 20 20 20 20
Ms. Beryl Ang’iro September 3, 2020 13 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A logical vector
Also;
> z <- (x+1)[(!is.na(x))&x>0]
>z
[1] 11.4 6.6 4.1 7.4 22.7 10.6 3.1 14.3 4.4
Creates an object z and places it in the values of the vector (x+1) for
which the corresponding value in x was both non-missing and positive.
It is possible to access the values of an element with a comparison
expression as the index.

For example;
> w <- 1:10
> w[w >= 5] <- 20
>w
[1] 1 2 3 4 20 20 20 20 20 20
Ms. Beryl Ang’iro September 3, 2020 13 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of positive integral quantities

In this case, the values in the index vector must lie in the set
{1,2,...,length(x)} .

The corresponding elements of the vector are selected and concatenated ,


in that order in the result.

The index vector can be of any length and the result is of the same length
as the index vector.

For example;
> x[6] ## is the 6th component of x

> x[1:10] ## selects the first 10 elements of x (assuming that


length(x)≥10)

Ms. Beryl Ang’iro September 3, 2020 14 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of positive integral quantities

In this case, the values in the index vector must lie in the set
{1,2,...,length(x)} .

The corresponding elements of the vector are selected and concatenated ,


in that order in the result.

The index vector can be of any length and the result is of the same length
as the index vector.

For example;
> x[6] ## is the 6th component of x

> x[1:10] ## selects the first 10 elements of x (assuming that


length(x)≥10)

Ms. Beryl Ang’iro September 3, 2020 14 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of positive integral quantities

In this case, the values in the index vector must lie in the set
{1,2,...,length(x)} .

The corresponding elements of the vector are selected and concatenated ,


in that order in the result.

The index vector can be of any length and the result is of the same length
as the index vector.

For example;
> x[6] ## is the 6th component of x

> x[1:10] ## selects the first 10 elements of x (assuming that


length(x)≥10)

Ms. Beryl Ang’iro September 3, 2020 14 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of positive integral quantities

In this case, the values in the index vector must lie in the set
{1,2,...,length(x)} .

The corresponding elements of the vector are selected and concatenated ,


in that order in the result.

The index vector can be of any length and the result is of the same length
as the index vector.

For example;
> x[6] ## is the 6th component of x

> x[1:10] ## selects the first 10 elements of x (assuming that


length(x)≥10)

Ms. Beryl Ang’iro September 3, 2020 14 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of positive integral quantities

In this case, the values in the index vector must lie in the set
{1,2,...,length(x)} .

The corresponding elements of the vector are selected and concatenated ,


in that order in the result.

The index vector can be of any length and the result is of the same length
as the index vector.

For example;
> x[6] ## is the 6th component of x

> x[1:10] ## selects the first 10 elements of x (assuming that


length(x)≥10)

Ms. Beryl Ang’iro September 3, 2020 14 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of negative integral quantities

Such an index vector specifies the values to be excluded rather than


included.

For example;
> a <- x[-(1:5)] ## gives a all BUT the first five elements of x

>a

[1] NA 21.7 9.6 2.1 13.3 -1.8 3.4

Also
> b <- x[c(-2,-4,6)] ## drops second, fourth and sixth elements of x

> b
[1] -3.0 5.6 6.4 21.7 9.6 2.1 13.3 -1.8 3.4
Ms. Beryl Ang’iro September 3, 2020 15 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of negative integral quantities

Such an index vector specifies the values to be excluded rather than


included.

For example;
> a <- x[-(1:5)] ## gives a all BUT the first five elements of x

>a

[1] NA 21.7 9.6 2.1 13.3 -1.8 3.4

Also
> b <- x[c(-2,-4,6)] ## drops second, fourth and sixth elements of x

> b
[1] -3.0 5.6 6.4 21.7 9.6 2.1 13.3 -1.8 3.4
Ms. Beryl Ang’iro September 3, 2020 15 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of negative integral quantities

Such an index vector specifies the values to be excluded rather than


included.

For example;
> a <- x[-(1:5)] ## gives a all BUT the first five elements of x

>a

[1] NA 21.7 9.6 2.1 13.3 -1.8 3.4

Also
> b <- x[c(-2,-4,6)] ## drops second, fourth and sixth elements of x

> b
[1] -3.0 5.6 6.4 21.7 9.6 2.1 13.3 -1.8 3.4
Ms. Beryl Ang’iro September 3, 2020 15 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of negative integral quantities

Such an index vector specifies the values to be excluded rather than


included.

For example;
> a <- x[-(1:5)] ## gives a all BUT the first five elements of x

>a

[1] NA 21.7 9.6 2.1 13.3 -1.8 3.4

Also
> b <- x[c(-2,-4,6)] ## drops second, fourth and sixth elements of x

> b
[1] -3.0 5.6 6.4 21.7 9.6 2.1 13.3 -1.8 3.4
Ms. Beryl Ang’iro September 3, 2020 15 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of negative integral quantities

Such an index vector specifies the values to be excluded rather than


included.

For example;
> a <- x[-(1:5)] ## gives a all BUT the first five elements of x

>a

[1] NA 21.7 9.6 2.1 13.3 -1.8 3.4

Also
> b <- x[c(-2,-4,6)] ## drops second, fourth and sixth elements of x

> b
[1] -3.0 5.6 6.4 21.7 9.6 2.1 13.3 -1.8 3.4
Ms. Beryl Ang’iro September 3, 2020 15 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings

This only applies where an object has a "names" attribute to identify its
components.
In this case a sub-vector of the "names" vector may be used in the same
way as the positive integral quantities.
Accessing the values of an object with names:
The "names" are labels of the elements of an object, and thus of mode
character.
They are generally optional attributes. There are several kinds of "names"
(names, colnames, rownames, dimnames).
The "names" of a vector are stored in a vector of the same length of the
object, and can be accessed with the function names.

Ms. Beryl Ang’iro September 3, 2020 16 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings

This only applies where an object has a "names" attribute to identify its
components.
In this case a sub-vector of the "names" vector may be used in the same
way as the positive integral quantities.
Accessing the values of an object with names:
The "names" are labels of the elements of an object, and thus of mode
character.
They are generally optional attributes. There are several kinds of "names"
(names, colnames, rownames, dimnames).
The "names" of a vector are stored in a vector of the same length of the
object, and can be accessed with the function names.

Ms. Beryl Ang’iro September 3, 2020 16 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings

This only applies where an object has a "names" attribute to identify its
components.
In this case a sub-vector of the "names" vector may be used in the same
way as the positive integral quantities.
Accessing the values of an object with names:
The "names" are labels of the elements of an object, and thus of mode
character.
They are generally optional attributes. There are several kinds of "names"
(names, colnames, rownames, dimnames).
The "names" of a vector are stored in a vector of the same length of the
object, and can be accessed with the function names.

Ms. Beryl Ang’iro September 3, 2020 16 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings

This only applies where an object has a "names" attribute to identify its
components.
In this case a sub-vector of the "names" vector may be used in the same
way as the positive integral quantities.
Accessing the values of an object with names:
The "names" are labels of the elements of an object, and thus of mode
character.
They are generally optional attributes. There are several kinds of "names"
(names, colnames, rownames, dimnames).
The "names" of a vector are stored in a vector of the same length of the
object, and can be accessed with the function names.

Ms. Beryl Ang’iro September 3, 2020 16 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings

This only applies where an object has a "names" attribute to identify its
components.
In this case a sub-vector of the "names" vector may be used in the same
way as the positive integral quantities.
Accessing the values of an object with names:
The "names" are labels of the elements of an object, and thus of mode
character.
They are generally optional attributes. There are several kinds of "names"
(names, colnames, rownames, dimnames).
The "names" of a vector are stored in a vector of the same length of the
object, and can be accessed with the function names.

Ms. Beryl Ang’iro September 3, 2020 16 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings


For example;
> x <- 1:3
> names(x)
NULL
> names(x) <- c("a","b","c")
>x
a b c
1 2 3
> names(x)
[1] "a" "b" "c"
> names(x) <- NULL
>x
[1] 1 2 3
Ms. Beryl Ang’iro September 3, 2020 17 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings


For example;
> x <- 1:3
> names(x)
NULL
> names(x) <- c("a","b","c")
>x
a b c
1 2 3
> names(x)
[1] "a" "b" "c"
> names(x) <- NULL
>x
[1] 1 2 3
Ms. Beryl Ang’iro September 3, 2020 17 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings


For example;
> x <- 1:3
> names(x)
NULL
> names(x) <- c("a","b","c")
>x
a b c
1 2 3
> names(x)
[1] "a" "b" "c"
> names(x) <- NULL
>x
[1] 1 2 3
Ms. Beryl Ang’iro September 3, 2020 17 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings


For example;
> x <- 1:3
> names(x)
NULL
> names(x) <- c("a","b","c")
>x
a b c
1 2 3
> names(x)
[1] "a" "b" "c"
> names(x) <- NULL
>x
[1] 1 2 3
Ms. Beryl Ang’iro September 3, 2020 17 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings


For example;
> x <- 1:3
> names(x)
NULL
> names(x) <- c("a","b","c")
>x
a b c
1 2 3
> names(x)
[1] "a" "b" "c"
> names(x) <- NULL
>x
[1] 1 2 3
Ms. Beryl Ang’iro September 3, 2020 17 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings


For example;
> x <- 1:3
> names(x)
NULL
> names(x) <- c("a","b","c")
>x
a b c
1 2 3
> names(x)
[1] "a" "b" "c"
> names(x) <- NULL
>x
[1] 1 2 3
Ms. Beryl Ang’iro September 3, 2020 17 / 19
Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings

Now, suppose;
> fruits <- c(5,10,1,20)
> names(fruits) <- c("orange","banana","apple","peach")
> fruits
orange banana apple peach
5 10 1 20
> lunch <- fruits[c("apple","orange")]
> lunch
apple orange
1 5

Ms. Beryl Ang’iro September 3, 2020 18 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings

Now, suppose;
> fruits <- c(5,10,1,20)
> names(fruits) <- c("orange","banana","apple","peach")
> fruits
orange banana apple peach
5 10 1 20
> lunch <- fruits[c("apple","orange")]
> lunch
apple orange
1 5

Ms. Beryl Ang’iro September 3, 2020 18 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings

Now, suppose;
> fruits <- c(5,10,1,20)
> names(fruits) <- c("orange","banana","apple","peach")
> fruits
orange banana apple peach
5 10 1 20
> lunch <- fruits[c("apple","orange")]
> lunch
apple orange
1 5

Ms. Beryl Ang’iro September 3, 2020 18 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings

Now, suppose;
> fruits <- c(5,10,1,20)
> names(fruits) <- c("orange","banana","apple","peach")
> fruits
orange banana apple peach
5 10 1 20
> lunch <- fruits[c("apple","orange")]
> lunch
apple orange
1 5

Ms. Beryl Ang’iro September 3, 2020 18 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

A vector of character strings

Now, suppose;
> fruits <- c(5,10,1,20)
> names(fruits) <- c("orange","banana","apple","peach")
> fruits
orange banana apple peach
5 10 1 20
> lunch <- fruits[c("apple","orange")]
> lunch
apple orange
1 5

Ms. Beryl Ang’iro September 3, 2020 18 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

Note:

An index expression can also appear on the receiving end of an


assignment, in which case the assignment operation is performed only on
those elements of the vector.
The expression must be of the form vector[index vector].
The vector assigned must match the length of the index vector, and in the
case of the logical index vector it must again be the same length as the
vector it is indexing.
For example;
> x[is.na(x)] <- 0
[1] -3.0 10.4 5.6 3.1 6.4 0.0 21.7 9.6 2.1 13.3 -1.8 3.4
Replaces any missing values in x by zeros.

Ms. Beryl Ang’iro September 3, 2020 19 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

Note:

An index expression can also appear on the receiving end of an


assignment, in which case the assignment operation is performed only on
those elements of the vector.
The expression must be of the form vector[index vector].
The vector assigned must match the length of the index vector, and in the
case of the logical index vector it must again be the same length as the
vector it is indexing.
For example;
> x[is.na(x)] <- 0
[1] -3.0 10.4 5.6 3.1 6.4 0.0 21.7 9.6 2.1 13.3 -1.8 3.4
Replaces any missing values in x by zeros.

Ms. Beryl Ang’iro September 3, 2020 19 / 19


Simple manipulations continuation
Index vectors, selecting and modifying subsets of a dataset

Note:

An index expression can also appear on the receiving end of an


assignment, in which case the assignment operation is performed only on
those elements of the vector.
The expression must be of the form vector[index vector].
The vector assigned must match the length of the index vector, and in the
case of the logical index vector it must again be the same length as the
vector it is indexing.
For example;
> x[is.na(x)] <- 0
[1] -3.0 10.4 5.6 3.1 6.4 0.0 21.7 9.6 2.1 13.3 -1.8 3.4
Replaces any missing values in x by zeros.

Ms. Beryl Ang’iro September 3, 2020 19 / 19

You might also like