Beginning Data Science in R 4 Data Analysis Visualization and Modelling For The Data Scientist Second Edition 2nd Edition Thomas Mailund Download
Beginning Data Science in R 4 Data Analysis Visualization and Modelling For The Data Scientist Second Edition 2nd Edition Thomas Mailund Download
https://ebookbell.com/product/beginning-data-science-in-r-4-data-
analysis-visualization-and-modelling-for-the-data-scientist-2nd-
edition-thomas-mailund-62608344
https://ebookbell.com/product/beginning-data-science-in-r-data-
analysis-visualization-and-modelling-for-the-data-scientist-thomas-
mailund-5755586
https://ebookbell.com/product/beginning-mathematica-and-wolfram-for-
data-science-applications-in-data-analysis-machine-learning-and-
neural-networks-2nd-edition-jalil-villalobos-alva-58283382
https://ebookbell.com/product/beginning-mathematica-and-wolfram-for-
data-science-applications-in-data-analysis-machine-learning-and-
neural-networks-jalil-villalobos-alva-22971926
Beginning Mathematica And Wolfram For Data Science Applications In
Data Analysis Machine Learning And Neural Networks Jalil Villalobos
Alva
https://ebookbell.com/product/beginning-mathematica-and-wolfram-for-
data-science-applications-in-data-analysis-machine-learning-and-
neural-networks-jalil-villalobos-alva-58302778
https://ebookbell.com/product/beginning-mathematica-and-wolfram-for-
data-science-applications-in-data-analysis-machine-learning-and-
neural-networks-58367918
https://ebookbell.com/product/beginning-data-science-with-python-and-
jupyter-use-powerful-industrystandard-tools-within-jupyter-and-the-
python-ecosystem-to-unlock-new-actionable-insights-from-your-data-
alex-galea-43824710
https://ebookbell.com/product/beginning-data-science-with-r-1st-
edition-manas-a-pathak-4973176
https://ebookbell.com/product/beginning-data-science-iot-and-ai-on-
single-board-computers-core-skills-and-realworld-application-with-the-
bbc-microbit-and-xinabox-1st-edition-philip-meitiner-11263736
Beginning Data
Science in R 4
Data Analysis, Visualization, and
Modelling for the Data Scientist
—
Second Edition
—
Thomas Mailund
Beginning Data Science
in R 4
Data Analysis, Visualization,
and Modelling for the Data Scientist
Second Edition
Thomas Mailund
Beginning Data Science in R 4: Data Analysis, Visualization, and Modelling for the
Data Scientist
Thomas Mailund
Aarhus, Denmark
Introduction������������������������������������������������������������������������������������������������������������xxi
iv
Table of Contents
v
Table of Contents
vi
Table of Contents
vii
Table of Contents
Numeric������������������������������������������������������������������������������������������������������������������������������� 291
Integer��������������������������������������������������������������������������������������������������������������������������������� 291
Complex������������������������������������������������������������������������������������������������������������������������������� 292
Logical��������������������������������������������������������������������������������������������������������������������������������� 292
Character����������������������������������������������������������������������������������������������������������������������������� 293
Data Structures������������������������������������������������������������������������������������������������������������������������� 294
Vectors��������������������������������������������������������������������������������������������������������������������������������� 294
Matrix���������������������������������������������������������������������������������������������������������������������������������� 296
Lists������������������������������������������������������������������������������������������������������������������������������������� 298
Indexing������������������������������������������������������������������������������������������������������������������������������� 300
Named Values���������������������������������������������������������������������������������������������������������������������� 304
Factors��������������������������������������������������������������������������������������������������������������������������������� 305
Formulas����������������������������������������������������������������������������������������������������������������������������� 305
Control Structures��������������������������������������������������������������������������������������������������������������������� 306
Selection Statements���������������������������������������������������������������������������������������������������������� 306
Loops����������������������������������������������������������������������������������������������������������������������������������� 307
Functions���������������������������������������������������������������������������������������������������������������������������������� 311
Named Arguments��������������������������������������������������������������������������������������������������������������� 312
Default Parameters�������������������������������������������������������������������������������������������������������������� 313
Return Values���������������������������������������������������������������������������������������������������������������������� 314
Lazy Evaluation�������������������������������������������������������������������������������������������������������������������� 315
Scoping�������������������������������������������������������������������������������������������������������������������������������� 317
Function Names Are Different from Variable Names����������������������������������������������������������� 322
Recursive Functions������������������������������������������������������������������������������������������������������������������ 322
Exercises����������������������������������������������������������������������������������������������������������������������������������� 325
Fibonacci Numbers�������������������������������������������������������������������������������������������������������������� 325
Outer Product���������������������������������������������������������������������������������������������������������������������� 325
Linear Time Merge��������������������������������������������������������������������������������������������������������������� 325
Binary Search���������������������������������������������������������������������������������������������������������������������� 326
More Sorting������������������������������������������������������������������������������������������������������������������������ 326
Selecting the k Smallest Element���������������������������������������������������������������������������������������� 327
viii
Table of Contents
ix
Table of Contents
NAMESPACE������������������������������������������������������������������������������������������������������������������������������ 399
R/ and man/������������������������������������������������������������������������������������������������������������������������� 400
Checking the Package�������������������������������������������������������������������������������������������������������������� 400
Roxygen������������������������������������������������������������������������������������������������������������������������������������ 401
Documenting Functions������������������������������������������������������������������������������������������������������� 401
Import and Export���������������������������������������������������������������������������������������������������������������� 402
Package Scope vs. Global Scope����������������������������������������������������������������������������������������� 404
Internal Functions���������������������������������������������������������������������������������������������������������������� 404
File Load Order�������������������������������������������������������������������������������������������������������������������� 404
Adding Data to Your Package���������������������������������������������������������������������������������������������������� 405
NULL������������������������������������������������������������������������������������������������������������������������������������ 406
Building an R Package�������������������������������������������������������������������������������������������������������������� 407
Exercises����������������������������������������������������������������������������������������������������������������������������������� 407
Conclusions���������������������������������������������������������������������������������������������������������� 501
D
ata Science����������������������������������������������������������������������������������������������������������������������������� 501
M
achine Learning��������������������������������������������������������������������������������������������������������������������� 501
D
ata Analysis���������������������������������������������������������������������������������������������������������������������������� 502
R
Programming������������������������������������������������������������������������������������������������������������������������� 502
T he End������������������������������������������������������������������������������������������������������������������������������������� 503
Index��������������������������������������������������������������������������������������������������������������������� 505
xiii
About the Author
Thomas Mailund is an associate professor in bioinformatics at Aarhus University,
Denmark. His background is in math and computer science, but for the last decade, his
main focus has been on genetics and evolutionary studies, particularly comparative
genomics, speciation, and gene flow between emerging species.
xv
About the Technical Reviewer
Jon Westfall is an associate professor of psychology at Delta
State University. He has authored Set Up and Manage Your
Virtual Private Server, Practical R 4, Beginning Android
Web Apps Development, Windows Phone 7 Made Simple,
and several works of fiction including One in the Same,
Mandate, and Franklin: The Ghost Who Successfully Evicted
Hipsters from His Home and Other Short Stories. He lives in
Cleveland, Mississippi, with his wife.
xvii
Acknowledgments
I would like to thank Asger Hobolth for many valuable comments on earlier versions of
this manuscript that helped me improve the writing and the presentation of the material.
xix
Introduction
Welcome to Beginning Data Science in R 4. I wrote this book from a set of lecture notes
for two classes I taught a few years back, “Data Science: Visualization and Analysis”
and “Data Science: Software Development and Testing.” The book is written to fit the
structure of these classes, where each class consists of seven weeks of lectures followed
by project work. This means that the book’s first half consists of eight chapters with core
material, where the first seven focus on data analysis and the eighth is an example of
a data analysis project. The data analysis chapters are followed by seven chapters on
developing reusable software for data science and then a second project that ties the
software development chapters together. At the end of the book, you should have a good
sense of what data science can be, both as a field covering analysis and developing new
methods and reusable software products.
xxi
Introduction
natural sciences, in this way. It isn’t so much about studying the natural world as it is about
computing efficiently on data and learning patterns from the data.
Included in data science is also the design of experiments. With the right data, we
can address the questions in which we are interested. This can be difficult with a poor
design of experiments or a poor choice of which data we gather. Study design might be
the most critical aspect of data science but is not the topic of this book. In this book, I
focus on the analysis of data, once gathered.
Computer science is mainly the study of computations, hinted at in the name, but is
a bit broader. It is also about representing and manipulating data. The name “computer
science” focuses on computation, while “data science” emphasizes data. But of course,
the fields overlap. If you are writing a sorting algorithm, are you then focusing on the
computation or the data? Is that even a meaningful question to ask?
There is considerable overlap between computer science and data science, and,
naturally, the skill sets you need overlap as well. To efficiently manipulate data, you
need the tools for doing that, so computer programming skills are a must, and some
knowledge about algorithms and data structures usually is as well. For data science,
though, the focus is always on the data. A data analysis project focuses on how the data
flows from its raw form through various manipulations until it is summarized in some
helpful way. Although the difference can be subtle, the focus is not on what operations
a program does during the analysis but how the data flows and is transformed. It is also
focused on why we do certain data transformations, what purpose those changes serve,
and how they help us gain knowledge about the data. It is as much about deciding what
to do with the data as it is about how to do it efficiently.
Statistics is, of course, also closely related to data science. So closely linked that many
consider data science as nothing more than a fancy word for statistics that looks slightly
more modern and sexy. I can’t say that I strongly disagree with this—data science does
sound hotter than statistics—but just as data science is slightly different from computer
science, data science is also somewhat different from statistics. Only, perhaps, somewhat
less so than computer science is.
A large part of doing statistics is building mathematical models for your data and
fitting the models to the data to learn about the data in this way. That is also what we
do in data science. As long as the focus is on the data, I am happy to call statistics data
science. But suppose the focus changes to the models and the mathematics. In that case,
we are drifting away from data science into something else—just as if the focus shifts
from the data to computations, we are straying from data science to computer science.
xxii
Introduction
xxiii
Introduction
I won’t expect you to know a lot about statistics and programming, but this isn’t
“Data Science for Dummies,” so I expect you to figure out examples without me
explaining everything in detail.
After the first seven chapters is a short description of a data analysis project that one
of my students did for my class the first time I held it. It shows how such a project could
look, but I suggest that you do not wait until you have finished the first seven chapters to
start doing such analysis yourself. To get the most benefit out of reading this book, you
should continuously apply what you learn. Already when you begin reading, I suggest
that you find a data set that you would be interested in finding out more about and then
apply what you learn in each chapter to that data.
For the following eight chapters of the book, the focus is on programming. To read
this part, you should be familiar with object-oriented programming—I will explain
how we handle it in R and how it differs from languages such as Python, Java, or C++.
Still, I will expect you to be familiar with terms such as class hierarchies, inheritance,
and polymorphic methods. I will not expect you to be already familiar with functional
programming (but if you are, there should still be plenty to learn in those chapters if you
are not already familiar with R programming). The final chapter is yet another project
description.
• Summarizing data
• Model building
These are the critical aspects of doing analysis in data science. After this, we will
cover how to develop R code that is reusable and works well with existing packages and
that is easy to extend, and we will see how to build new R packages that other people
will be able to use in their projects. These are the essential skills you will need to develop
your own methods and share them with the world.
xxiv
Introduction
R is one of the most popular (and open source) data analysis programming
languages around at the moment. Of course, popularity doesn’t imply quality. Still,
because R is so popular, it has a rich ecosystem of extensions (called “packages” in R) for
just about any kind of analysis you could be interested in. People who develop statistical
methods often implement them as R packages, so you can usually get the state-of-the-art
techniques very easily in R. The popularity also means that there is a large community
of people who can help if you have problems. Most problems you run into can be solved
with a few minutes on Google or Stack Overflow because you are unlikely to be the first
to run into any particular issue. There are also plenty of online tutorials for learning
more about R and specialized packages. And there are plenty of books you can buy if you
want to learn more.
5. Working with Large Data Sets: In this chapter, we see how to deal
with data where the number of observations makes our usual
approaches too slow.
xxv
Introduction
Software Development
The next nine chapters cover software and package development:
xxvi
Introduction
P
rojects
You cannot learn how to analyze data without analyzing data, and you cannot
understand how to develop software without developing software either. Typing in
examples from the book is nothing like writing code on your own. Even doing exercises
from the book—which you really ought to do—is not the same as working on your own
projects. Exercises, after all, cover minor isolated aspects of problems you have just been
introduced to. There is not a chapter of material presented before every task you have to
deal with in the real world. You need to work out by yourself what needs to be done and
how. If you only do the exercises in this book, you will miss the most crucial lesson in
analyzing data:
• How to explore the data and get a feeling for it
And for developing a package, you need to think through how to design and
implement its functionality such that the various functions and data structures fit well
together.
I will go through a data analysis project to show you what that can look like in this
book. To learn how to analyze data on your own, you need to do it yourself as well—and
you need to do it with a data set that I haven’t explored for you. You might have a data
set lying around you have worked on before, a data set from something you are just
xxvii
Introduction
interested in, or you can probably find something interesting at a public data repository,
for example, one of these:
• RDataMining.com: www.rdatamining.com/resources/data
I suggest that you find yourself a data set and that you, after each lesson, use the
skills you have learned to explore this data set. Pick data structured as a table with
observations as rows and variables as columns since that is the form of the data we will
consider in this book. At the end of the first eight chapters, you will have analyzed this
data. You can write a report about your analysis that others can evaluate to follow and
maybe modify it: you will be doing reproducible science.
For the programming topics, I will describe another project illustrating the design
and implementation issues involved in making an R package. There, you should be able
to learn from implementing your own version of the project I use, but you will, of course,
be more challenged by working on a project without any of my help at all. Whatever you
do, to get the full benefit of this book, you really ought to make your own package while
reading the programming chapters.
xxviii
CHAPTER 1
Introduction to R
Programming
We will use R for our data analysis, so we need to know the basics of programming in
the R language. R is a full programming language with both functional programming
and object-oriented programming features, and learning the complete language is
far beyond the scope of this chapter. We return to it later, when we have a little more
experience using R. The good news is, though, that to use R for data analysis, we rarely
need to do much programming. At least, if you do the right kind of programming, you
won’t need much.
For manipulating data—how to do this is the topic of the next chapter—you mainly
have to string together a couple of operations, such as “group the data by this feature”
followed by “calculate the mean value of these features within each group” and then
“plot these means.” Doing this used to be more complicated to do in R, but a couple of
new ideas on how to structure data flow—and some clever implementations of these in
packages such as magrittr and dplyr—have significantly simplified it. We will see some
of this at the end of this chapter and more in the next chapter. First, though, we need to
get a taste of R.
1
© Thomas Mailund 2022
T. Mailund, Beginning Data Science in R 4, https://doi.org/10.1007/978-1-4842-8155-0_1
Chapter 1 Introduction to R Programming
project. You can create a project from an existing directory, but if this is the first time you
are working with R, you probably just want to create an empty project in a new directory,
so do that.
Once you have RStudio opened, you can type R expressions into the console, which
is the frame on the left of the RStudio window. When you write an expression there, R
will read it, evaluate it, and print the result. When you assign values to variables, and
we will see how to do this shortly, they will appear in the Environment frame on the top
right. At the bottom right, you have the directory where the project lives, and files you
create will go there.
To create a new file, you go to File and then New File…. There you can select
several different file types. Those we are interested in are the R Script, R Notebook,
and R Markdown types. The former is the file type for pure R code, while the latter two
we use for creating reports where documentation text is mixed with R code. For data
analysis projects, I would recommend using either Notebook or Markdown files. Writing
2
Chapter 1 Introduction to R Programming
documentation for what you are doing is helpful when you need to go back to a project
several months down the line.
For most of this chapter, you can just write R code in the console, or you can create
an R Script file. If you create an R Script file, it will show up on the top left; see Figure 1-2.
You can evaluate single expressions using the Run button on the top right of this frame
or evaluate the entire file using the Source button. For writing longer expressions, you
might want to write them in an R Script file for now. In the next chapter, we will talk
about R Markdown, which is the better solution for data science projects.
Using R As a Calculator
You can use the R console as a calculator where you type in an expression you want to
calculate, hit “enter,” and R gives you the result. You can play around with that a little bit
to get familiar with how to write expressions in R—there is some explanation for how to
write them in the following—and then moving from using R as a calculator to writing
3
Chapter 1 Introduction to R Programming
more sophisticated analysis programs is only a matter of degree. A data analysis program
is little more than a sequence of calculations, after all.
Simple Expressions
Simple arithmetic expressions are written, as in most other programming languages, in
the typical mathematical notation that you are used to:
1 + 2
## [1] 3
4 / 2
## [1] 2
(2 + 2) * 3
## [1] 12
Here, the lines that start with ## show the output that R will give you. By convention,
and I don’t really know why, these two hash symbols are often used to indicate that in R
documentation.
It also works pretty much as you are used to, except, perhaps, that you might be used
to integers behaving as integers in a division. At least in some programming languages,
division between integers is integer division, but in R you can divide integers, and if
there is a remainder, you will get a floating-point number back as the result:
4 / 3
## [1] 1.333333
When you write numbers like 4 and 3, they are always interpreted as floating-point
numbers, even if they print as integers, that is, without a decimal point. To explicitly get
an integer, you must write 4L and 3L:
class(4)
## [1] "numeric"
class(4L)
## [1] "integer"
4
Chapter 1 Introduction to R Programming
4 %/% 3
## [1] 1
In many languages, % is used for getting the remainder of a division, but this doesn’t
quite work with R where % is used for something else (creating new infix operators), so in
R the operator for this is %%:
4 %% 3
## [1] 1
2 ^ 2
## [1] 4
2 ** 2
## [1] 4
2 ^ 3
## [1] 8
2 ** 3
## [1] 8
There are some other data types besides numbers, but we won’t go into an
exhaustive list here. There are two types you do need to know about early, though, since
they are frequently used and since not knowing about how they work can lead to all
kinds of grief. Those are strings and “factors.”
5
Chapter 1 Introduction to R Programming
Strings work as you would expect. You write them in quotes, either double quotes or
single quotes, and that is about it:
"Hello,"
## [1] "Hello,"
'world!'
## [1] "world!"
Strings are not particularly tricky, but I mention them because they look a lot like
factors, but factors are not like strings, they just look sufficiently like them to cause some
confusion. I will explain the factors a little later in this chapter when we have seen how
functions and vectors work.
Assignments
To assign a value to a variable, you use the arrow operators. So to assign the value 2 to
the variable x, you would write
x <- 2
and you can test that x now holds the value 2 by evaluating x:
## [1] 2
2 * x
## [1] 4
You can assign with arrows in both directions, so you could also write
2 -> x
6
Chapter 1 Introduction to R Programming
An assignment won’t print anything if you write it into the R terminal, but you can
get R to print it by putting the assignment in parentheses:
x <- "invisible"
(y <- "visible")
## [1] "visible"
1:50
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
## [16] 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
## [31] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
## [46] 46 47 48 49 50
The : operator creates a sequence of numbers, starting at the number to the left of
the colon and increasing by one until it reaches the number to the right of the colon, or
just before if an increment of one would move past the last number:
-1:1
## [1] -1 0 1
0.1:2.9
7
Chapter 1 Introduction to R Programming
If you want other increments than 1, you can use the seq function instead:
Here, the first number is where we start, the second where we should stop, as with :,
but the third number gives us the increment to use.
Because we are practically always working on vectors, there is one caveat I want
to warn you about. If you want to know the length of a string, you might—reasonably
enough—think you can get that using the length function. You would be wrong. That
function gives you the length of a vector, so if you give it a single string, it will always
return 1:
length("qax")
## [1] 1
length("quux")
## [1] 1
length(c("foo", "bar"))
## [1] 2
In the last expression, we used the function c() to concatenate two vectors of strings.
Concatenating "foo" and "bar"
c("foo", "bar")
creates a vector of two strings, and thus the result of calling length on that is 2. To get
the length of the actual string, you want nchar instead:
nchar("qax")
## [1] 3
nchar("quux")
## [1] 4
8
Chapter 1 Introduction to R Programming
nchar(c("foo", "bar"))
## [1] 3 3
If you wanted to concatenate the strings "foo" and "bar", to get a vector with the
single string "foobar", you need to use paste:
## [1] "foobar"
The argument sep = "" tells paste not to put anything between the two strings. By
default, it would put a space between them:
paste("foo", "bar")
Indexing Vectors
If you have a vector and want the i’th element of that vector, you can index the vector to
get it like this:
(v <- 1:5)
## [1] 1 2 3 4 5
v[1]
## [1] 1
v[3]
## [1] 3
We have parentheses around the first expression to see the output of the operation.
An assignment is usually silent in R, but by putting the expression in parentheses, we
make sure that R prints the result, which is the vector of integers from 1 to 5. Notice here
that the first element is at index 1. Many programming languages start indexing at zero,
but R starts indexing at one. A vector of length n is thus indexed from 1 to n, unlike in
zero-indexed languages where the indices go from 0 to n − 1.
9
Chapter 1 Introduction to R Programming
If you want to extract a subvector, you can also do this with indexing. You just use a
vector of the indices you want inside the square brackets. We can use the : operator for
this or the concatenate function, c():
v[1:3]
## [1] 1 2 3
v[c(1,3,5)]
## [1] 1 3 5
You can use a vector of boolean values to pick out those values that are “true”:
## [1] 1 3 5
This indexing is particularly useful when you combine it with expressions. We can,
for example, get a vector of boolean values telling us which values of a vector are even
numbers and then use that vector to pick them out:
v %% 2 == 0
v[v %% 2 == 0]
## [1] 2 4
You can get the complement of a vector of indices if you change the sign of them:
v[-(1:3)]
## [1] 4 5
It is also possible to give vector indices names, and if you do, you can use those to
index into the vector. You can set the names of a vector when constructing it or use the
names function:
## A B C
10
Chapter 1 Introduction to R Programming
## 1 2 3
v["A"]
## A
## 1
## x y z
## 1 2 3
v["x"]
## x
## 1
Names can be handy for making tables where you can look up a value by a key.
Vectorized Expressions
Now, the reason that the expressions we saw earlier worked with vector values instead
of single values is that in R, arithmetic expressions all work component-wise on vectors.
When you write an expression such as
## [1] -3 -1 3
you are telling R to take each element in the vector x, squaring it, and subtracting
element-wise by y:
(x <- 1:3)
## [1] 1 2 3
x ** 2
## [1] 1 4 9
y <- 6:8
11
Chapter 1 Introduction to R Programming
x ** 2 - y
## [1] -5 -3 1
This also works if the vectors have different lengths, as they do in the preceding
example. The vector 2 is a vector of length 1 containing the number 2. The way
expressions work, when vectors do not have the same length, is you repeat the shorter
vector as many times as you need to:
(x <- 1:4)
## [1] 1 2 3 4
(y <- 1:2)
## [1] 1 2
x - y
## [1] 0 0 2 2
If the length of the longer vector is not a multiple of the length of the shorter, you get
a warning. The expression still repeats the shorter vector a number of times, just not an
integer number of times:
(x <- 1:4)
## [1] 1 2 3 4
(y <- 1:3)
## [1] 1 2 3
x - y
## [1] 0 0 0 3
Here, y is used once against the 1:3 part of x, and the first element of y is then used
for the 4 in x.
12
Chapter 1 Introduction to R Programming
Comments
You probably don’t want to write comments when you are just interacting with the
R terminal, but in your code, you do. Comments let you describe what your code is
intended to do, and how it is achieving it, so you don’t have to work that out again when
you return to it at a later point, having forgotten all the great thoughts you thought when
you wrote it.
R interprets as comments everything that follows the # character. From a # to the end
of the line, the R parser skips the text:
# This is a comment.
If you write your analysis code in R Markdown documents, which we will cover in the
next chapter, you won’t have much need for comments. In those kinds of files, you mix
text and R code differently. But if you develop R code, you will likely need it, and now you
know how to write comments.
Functions
You have already seen the use of functions, although you probably didn’t think much
about it when we saw expressions such as
length("qax")
You didn’t think about it because there wasn’t anything surprising about it. We just
use the usual mathematical notation for functions: f (x). If you want to call a function,
you simply use this notation and give the function its parameters in parentheses.
In R, you can also use the names of the parameters when calling a function, in
addition to the positions; we saw an example with sep = "" when we used paste to
concatenate two strings.
If you have a function f (x, y) of two parameters, x and y, calling f (5, 10) means calling
f with parameter x set to 5 and parameter y set to 10. In R, you can specify this explicitly,
and these two function calls are equivalent:
f(5, 10)
f(x = 5, y = 10)
13
Chapter 1 Introduction to R Programming
(Don’t try to run this code; we haven’t defined the function f, so calling it will fail.
But if we had a function f, then the two calls would be equivalent.)
If you specify the names of the parameters, the order doesn’t matter anymore, so
another equivalent function call would be
f(y = 10, x = 5)
You can combine the two ways of passing parameters to functions as long as you put
all the positional parameters before the named ones:
f(5, y = 10)
Except for maybe making the code slightly more readable—it is usually easier to
remember what parameters do than which order they come in—there is not much need
for this in itself. Where it becomes useful is when combined with default parameters.
A lot of functions in R take many parameters. More than you really can remember
the use for and certainly the order of. They are a lot like programs that take a lot of
options but where you usually just use the defaults unless you need to tweak something.
These functions take a lot of parameters, but most of them have useful default values,
and you typically do not have to specify the values to set them to. When you do need it,
though, you can specify it with a named parameter.
14
Chapter 1 Introduction to R Programming
Try looking up the documentation for a few functions, for example, the nchar
function we also saw earlier.
All infix operators, like + or %%, are also functions in R, and you can read the
documentation for them as well. But you cannot write ?+ in the R terminal and get the
information. The R parser doesn’t know how to deal with that. If you want help on an
infix operator, you need to quote it, and you do that using back quotes. So to read the
documentation for +, you would need to write
?`+`
You probably do not need help to figure out what addition does, but people can write
new infix operators, so this is useful to know when you need help with those.
15
Chapter 1 Introduction to R Programming
## [1] 4
square(1:4)
## [1] 1 4 9 16
The “function(x) x**2” expression defines the function, and anywhere you would
need a function, you can write the function explicitly like this. Assigning the function to
a name lets you use the name to refer to the function, just like assigning any other value,
like a number or a string to a name, will let you use the name for the value.
Functions you write yourself work just like any function already part of R or part of
an R package, with one exception, though: you will not have documentation for your
functions unless you write it, and that is beyond the scope of this chapter (but covered in
the chapter on building packages).
The square function just does a simple arithmetic operation on its input. Sometimes,
you want the function to do more than a single thing. If you want the function to do
several operations on its input, you need several statements for the function. In that case,
you need to give it a “body” of several statements, and such a body has to go in curly
brackets:
## [1] -4 0 6 14 24
(Check the documentation for rev to see what is going on here. Make sure you
understand what this example is doing.)
16
Chapter 1 Introduction to R Programming
In this simple example, we didn’t really need several statements. We could just have
written the function as
As long as there is only a single expression in the function, we don’t need the curly
brackets. For more complex functions, you will need it, though.
The result of a function—what it returns as its value when you call it—is the last
statement or expression (there actually isn’t any difference between statements and
expressions in R; they are the same thing). You can make the return value explicit,
though, using the return() expression:
Explicit returning is usually only used when you want to return a value before the
end of the function. To see examples of this, we need control structures, so we will have
to wait a little bit to see an example. It isn’t used as much as in many other programming
languages.
One crucial point here, though, if you are used to programming in other languages:
The return() expression needs to include the parentheses. In most programming
languages, you could just write
Such an expression doesn’t work for R. Try it, and you will get an error.
17
Chapter 1 Introduction to R Programming
Take, for example, the function sum which adds together all the values in a vector you
give it as an argument (check ?sum now to see the documentation):
sum(1:4)
## [1] 10
This function summarizes its input into a single value. There are many similar
functions, and, naturally, these cannot be used element-wise on vectors; rather, they
reduce an entire vector into some smaller summary statistics, here the sum of all
elements.
Whether a function works on vector expressions or not depends on how it is defined.
While there are exceptions, most functions in R either work on vectors or summarize
vectors like sum. When you write your own functions, whether the function works
element-wise on vectors or not depends on what you put in the body of the function. If
you write a function that just does arithmetic on the input, like square, it will work in
vectorized expressions. If you write a function that does some summary of the data, it
will not. For example, if we write a function to compute the average of its input like this:
## [1] 3
This function will not give you values element-wise. Pretty obviously. It gets a little
more complicated when the function you write contains control structures, which we
will get to in the next section. In any case, this would be a nicer implementation since it
only involves one expression:
Oh, and by the way, don’t use this average function to compute the mean value of a
vector. R already has a function for that, mean, that deals much better with special cases
like missing data and vectors of length zero. Check out ?mean.
18
Chapter 1 Introduction to R Programming
Just because you are summarizing doesn’t mean that you have to return a single
value. In this function, we return both the mean and the standard deviation of the values
in a vector:
## mean sd
## 5.50000 3.02765
We use the functions mean and sd to compute the two summary statistics, and
then we combine them into a vector (with named elements) that contains the two
summaries. This isn’t a vectorized function, because we do not process the values in
the input element-wise. It doesn’t compute a single summary, but returns something
(ever so slightly) more complex. Complicated functions often return data more complex
than vectors or single values, and we shall see examples in later chapters. If you can
avoid it, though, do so. Simple functions, with simple input and output, are easier to
use, and when we write functions, we want to make things as simple for us as we can.
With this mean_and_sd function, we do not gain anything that we do not already have
with the mean and sd function, and combining both operations in a single function only
complicates things needlessly.
The rough classification of functions into the vectorized, which operate element-
wise on data, and the summarizing functions, is only a classification of how we can use
them. If you compute a value for each element in one or more vectors, you have the
former, and if you summarize all the data in one or more vectors, you have the latter. The
implementation of a function can easily combine both.
Imagine, for example, that we wish to normalize data by subtracting the mean from
each element and then dividing by the standard deviation. We could implement it
like this:
19
Chapter 1 Introduction to R Programming
We compute a value for each element in the input, so we have a vectorized function,
but in the implementation, we use two summarizing functions, mean and sd. The
expression (x - mean(x)) / sd(x) is a vector expression because mean(x) and sd(x)
become vectors of length one, and we can use those in the expression involving x to get a
value for each element.
# this will
if (3 > 2) "true"
## [1] "true"
For expressions like these, where we do not alter the program state by evaluating
the expression, there isn’t much of an effect in evaluating the if expression. If we, for
example, are assigning to a variable, there will be an effect:
x <- "foo"
if (2 > 3) x <- "bar"
x
## [1] "foo"
## [1] "baz"
20
Chapter 1 Introduction to R Programming
If you want to have effects for both true and false expressions, you have this:
## [1] "baz"
If you want newlines in if statements, whether you have an else part or not, you
should use curly brackets.
You don’t always have to. If you have a single expression in the if part, you can leave
them out:
if (3 > 2)
x <- "bar"
x
## [1] "bar"
or if you have a single statement in the else part, you can leave out the brackets:
if (2 > 3) {
x <- "bar"
} else
x <- "qux"
x
## [1] "qux"
but we did need the brackets in the preceding if part for R to recognize that an else
bit was following. Without it, we would get an error:
if (2 > 3)
x <- "bar"
else
x <- "qux"
21
Chapter 1 Introduction to R Programming
If you always use brackets, you don’t have to worry about when you strictly need
them or when you do not, and a part can have multiple statements without you having
to worry about it. If you put a newline in an if or if-else expression, I recommend that
you always use brackets as well.
An if statement works like an expression:
## [1] "baz"
This evaluates to the result of the expression in the “if” or the “else” part, depending
on the truth value of the condition:
## [1] "baz"
## [1] "baz"
but when the entire statement is on a single line, and the two parts are both a single
expression, I usually do not bother with that.
You cannot use it for vectorized expressions, though, since the boolean expression, if
you give it a vector, will evaluate the first element in the vector:
x <- 1:5
if (x > 3) "bar" else "baz"
## [1] "baz"
If you want a vectorized version of if statements, you can instead use the ifelse()
function:
22
Chapter 1 Introduction to R Programming
x <- 1:5
ifelse(x > 3, "bar", "baz")
## [1] 1 2 3 4 5
This function was supposed to square even numbers, and it will if we give it a single
number, but we gave it a vector. Since the first value in this vector, the only one that the
if statement looked at, was 1, it decided that x %% 2 == 0 was false—it is if x[1] is 1—
and then none of the values were squared. Clearly not what we wanted, and the warning
was warranted.
If you want a vectorized function, you need to use ifelse():
## [1] 1 4 3 16 5
23
Chapter 1 Introduction to R Programming
or you can use the Vectorize() function to translate a function that isn’t vectorized
into one that is:
## [1] 1 4 3 16 5
x <- 1:5
total <- 0
for (element in x) total <- total + element
total
## [1] 15
As with if statements, if you want the body to contain more than one expression,
you need to put it in curly brackets.
The for statement runs through the elements of a vector. If you want the indices
instead, you can use the seq_along() function, which given a vector as input returns a
vector of indices:
x <- 1:5
total <- 0
24
Chapter 1 Introduction to R Programming
## [1] 15
There are also while statements for looping. These repeat as long as an expression
is true:
x <- 1:5
total <- 0
index <- 1
while (index <= length(x)) {
element <- x[index]
index <- index + 1
total <- total + element
}
total
## [1] 15
If you are used to zero-indexed vectors, pay attention to the index <= length(x)
here. You would normally write index < length(x) in zero-indexed languages. Here,
that would miss the last element.
There is also a repeat statement that loops until you explicitly exit using the break
statement:
x <- 1:5
total <- 0
index <- 1
repeat {
element <- x[index]
total <- total + element
index <- index + 1
if (index > length(x)) break
}
total
## [1] 15
25
Random documents with unrelated
content Scribd suggests to you:
Elementary phenomena of magnetism and electricity, 483.
Elswick 4·7–in. gun, 206.
guns, 194.
Energy, 806.
Ether, 735.
the luminiferous, 408.
Exhaustion of coal, 755, 756, 757.
Expansive working of steam, 8, 17.
Explosion by concussion, 745.
of locomotive, 21.
of torpedoes, 229.
Explosives, 225, 740.
different effects of, 748.
names and classes of, 750.
Eye, the, 451.
dimensions of some parts of, 462.
Eye not optically perfect, 462.
Eyeballs, muscles of, 461.
F.
Fairbairn, Sir W., 280.
Faraday, 506, 508, 735.
ventilating gas-burner, 773.
Faure’s accumulator, 530.
Fellahs, 255.
Ferris wheel, Chicago, 81.
Field telegraphs, 555.
Fire-arms, 169.
Fish-plates, 105.
Fizeau, 386.
Floating matter in air, 383.
Fluids, electric, 487.
Fly-wheels, 7.
Force, conservation of, 804.
electromotive, 494.
Forth Bridge, the, 311.
Foucault, 387.
Fovea centralis, 456, 457.
Fraser-Woolwich guns, 195.
Fraunhofer’s lines, 420, 436.
Fresnel’s mirrors, 409.
measurement of velocity of light, 600.
Fribourg Suspension Bridge, 286.
Froment’s dial telegraph, 567.
Furnace, electric, 322.
G.
Galvanic batteries, 493, 494.
Galvanometer, 493.
mirror, 570.
Gas engine, 25.
governor, 769.
holder, 766.
making apparatus, 765.
meters, 775.
pressure, 769.
retorts, 766.
Gases of blast furnace, 49.
Gatling battery gun, or mitrailleur, 219.
Gauge, broad and narrow, 106.
Bourdon’s pressure, 12.
Geissler’s tubes, 505.
Ghost, Pepper’s, 392.
Giffard’s injector, 11.
Girder bridges, 280.
Glass, strains in, 407.
Glatton, H. M. S., 161.
Glynde, electric railway, 534.
Gold, 686.
Gold and diamonds, 687.
Gold-mining operations, 690.
Goodyear, Mr., 727.
Governor of steam engines, 6.
Gower Street Station, 114.
Gramme magneto-electric machine, the, 511.
Graphophone, 672.
Graphotype, 644.
Gray, 590.
Great Brooklyn Bridge, 303.
Great Eastern, 133, 152, 330, 465, 578.
Greatest Discovery of the Age, 801.
Greener’s expanding bullet, 182.
Grove, Sir W. R., 804.
Grove’s battery, 495.
Gun, 32–pounder, 191.
68–pounder, 192.
35–ton, 201.
81–ton, 201.
100–ton, 201.
110–ton, 202.
Elswick 4·7–in., 206.
Maxim, 225.
Moncrieff, 208.
Nordenfelt, 223.
Gun-cotton, 747.
torpedoes, 233.
Gunpowder, 734.
Guns, Armstrong’s, 192.
Elswick, 194.
Fraser-Woolwich, 195.
Krupp’s, 214.
quick-firing, 206.
submarine, 240.
Gutta-percha, 728.
H.
Half-tone process, 629.
Hancock, Mr. Charles, 729.
Mr. Thomas, 725.
Harvey’s torpedoes, 234.
Heat produced by electric current, 502.
Heat spectrum, 613.
Heating by gas, 776.
Helmholtz, 462, 464, 472, 474.
Henry, on Leyden jar discharge, 538.
Hercules, H.M.S., 150.
Hertz, Professor, 541.
Hippocampus, 664.
Hoe’s printing machines, 316, 318.
Holmes’ magneto-electric machine, 520.
Holophotal light, 604.
Holyhead and Kingston steamers, 136.
Horseless carriages (Automobiles), 23.
Horse-power, 10.
Hot-blast, 48.
Hotchkiss quick-firing guns, 208.
Hough’s metereograph, 654.
Howitzers, 213.
Hudson River steam navigation, 147.
Hughes’ printing telegraph, 560.
microphone, 590.
Hydraulic power, 324.
I.
Iceland spar, 399.
Illuminating power of gas, 774.
Illusion by movement of eye, 475.
by persistence of vision, 476.
stage, 290.
Images formed by lenses, 399, 616.
Impact theory of Stellar Evolution, 718.
Incandescent electric light, 528.
Incandescent gas-burners, 777.
Inclined railways, 125.
Inconstant, H.M.S., 152.
India-rubber, 724.
India-rubber and gutta-percha, 724.
Indicator, 9.
Induced currents, 502.
Induction coils, 503.
Injector, Giffard’s, 11.
Instantaneous photography, 623.
Introduction, 1.
Iron, 29.
Iron bridges, 276.
Iron in architecture, 72.
Iron, cast, 40.
chemical changes of, 34.
lighthouses, 596.
meteoric, 32.
ores, 39.
pig, 40.
ships, 133.
smelting, 39.
utility of, 30.
wrought, 47.
Ismaïlia, 260.
J.
Jablochkoff’s electric candle, 525.
Jackson, 551.
Jacobi, 531.
Jamin’s magnet, 513.
Johannesburg, 694.
Joule, 804, 805.
Joy’s valve gear, 20.
Jupiter, 384.
K.
Kaleidoscope, 389.
Karoos, 705.
Kimberley, 705.
Kinetographic theatre, 479.
Kinetoscope, 478.
Kirchhoff, 422.
Klondyke, 692.
König or Kaiser Wilhelm, ironclad, 164.
König’s printing machine, 308.
Krupp’s guns, 214.
steel, 55.
works, 56.
L.
Lake Timsah, 260.
Lathe at Woolwich, 198.
Lathe, Blanchard, 96.
screw-cutting, 87.
Lap of slide-valve, 9.
Lebel rifle, 188.
Lens, formation of image by, 616.
in steps, 600.
photographic, 616.
Lepidosiren, 685.
Letterpress printing, 306.
Leyden jar, 490.
discharge, 538.
Light, 380.
electric, 497.
invisible, 383.
Lighthouses, 593.
Limiting angle, 399.
Link motion, 16.
Linotype, the, 645.
Lithium, 425.
Lithography, 636.
Liverpool and Manchester Railway, 14.
Lock-gates, 267.
Locks in Manchester Ship Canal, 264, 266.
Locomotive, the, 14.
balancing of, 20.
compound, 18.
Lodge, Professor O., 540.
M.
Machine-guns, 218.
Madder, 796.
Magazine rifles, 187.
Magnesium, 720.
Magnetic field, 537.
Magnetism produced by current, 500.
Magneto-electric machines, 496, 507, 508.
Magneto-electricity, 506.
Mallet’s Mortars, 212.
Malus, 405.
Manchester Ship Canal, 262.
Manganese, 43.
Manhattan Life Insurance buildings, 78.
Mannlicher rifle, 187, 189.
Manufacturing v. making, 85.
Map, Channel Tunnel, 364.
Manchester Ship Canal, 263.
North Sea Canal, 271.
Pacific Railway, 117.
St. Gothard Railway, 372.
Suez Canal, 256.
Tower Bridge, etc., 299.
Marconi, 546.
Martini-Henry rifle, 184.
Mary Powell, the, 148.
Matter indestructible, 576.
Mauser rifle, 187, 188.
Maxim gun, 225.
Measuring machines, 88.
Mélinite, 748.
Menai Straits bridges, 280, 284.
Meteoric iron, 32.
Meteorites, 30.
Meteorology, importance of, 664.
Metereographs, 654.
Meters, gas, 775.
Metropolitan Railway, the, 114.
Microphone, 481.
Minié bullet, 180.
Mineral combustibles, 751.
Mines, submarine, 241.
Mirror, galvanometer, 570.
Mirrors, plane, 388.
Mirrors, illusions, 391–395.
Mitrailleur, 218.
Molecules, 733, 743.
Monarch, H.M.S., 156.
Moncrieff’s gun carriages, 208.
Mont Cenis Tunnel, 351.
Montigny mitrailleur, 222.
Morse’s code, 560.
instruments, 558.
plate, 562.
telegraphic line, 556.
transmitting key, 561.
Mortar, Mallet’s, 212.
Mount Washington Inclined Railway, 125.
Musical sound, 666.
N.
Naphthaline, 669.
Napier’s platen machine, 321.
Nasmyth’s steam hammer, 26.
Nature knowledge, 1.
printing, 640.
Nebulæ, 626.
Needle telegraphs, 553.
Negretti and Zambra’s recording thermometer, 659.
New metals, 714.
New York, the, 148.
Newton’s prism experiment, 418.
Niagara Suspension Bridge, 287.
Falls, 537.
Nicaragua Canal, 274.
Nichol’s prism, 403.
Niepce, J. N., 609.
de Saint-Victor, 611, 615.
Nitro-benzol, 784.
Nitrogen and oxygen compounds, 732.
Nitro-glycerine, 734.
Nordenfelt gun, 223.
North Sea Canal, 271.
Note A—Production of coal, 812.
Note B—Conservation of energy, 812.
O.
Œrsted’s experiments, 548.
Oil springs, 760.
Oldbury, manufacture of aluminium, 722.
Ophthalmoscope, 423.
Optical apparatus of lighthouses, 598.
Orders of lighthouse apparatus, 602.
Organic bodies, 798.
Oscillating engines, 14
P.
Pacific Railway, the, 116.
Paddle-wheels, 130.
Page, Mr., 532.
Panama Canal, 272.
Papier-maché stereotype process, 633.
Paraffin, 761.
oils, 762, 763.
Parallel motion, 8.
Paris Exhibition, buildings of, 76.
Pascal’s principle, 325.
Pattern printing, 321.
Pepper, J. H., 392, 393, 505.
Percussion cap, 180.
Petroleum, 757.
Phenakistiscope, 476.
Phenomena of light, some, 382.
Phonautograph, 666.
Phonograph, 665.
Photographic camera, 615.
Photography, 607.
Photography, celestial, 636.
in colours, 614, 630.
in the dark, 613.
X-Ray, 447.
Photolithography, 644.
Photozincography, 644.
Pig iron, 40.
Planes, Whitworth’s, 94.
Planets, photographs of, 636.
Planing machines, 92.
Plants in coal measures, 753.
Plaster of Paris, stereotype process, 633.
Pneumatic dispatch, 340.
force, 333.
Pniel, 701.
Points, railway, 108.
Polariscope, 405.
Polarizer, 403.
Polytechnic institution, Regent Street, 505.
Portable engines, 24.
Portable telegraphic instruments, 556.
Portrait, Davy, 714.
Helmholtz, 452.
Joule, 789.
Kirchhoff, 416.
Lesseps, 249.
Morse, 547.
Senefelder, 632.
Simpson, 731.
Tesla, 572.
Thomson, 481.
Watt, 3.
Whitworth, 85.
Port Saïd, 257.
Post-office railway van, 111.
Potassium, 715.
Powder, smokeless, 748.
Power, horse, 10.
hydraulic, 324.
of steam engine, 9.
of locomotive, 21.
Powers, mechanical, 32.
Pressure gauge, 12.
transmitted in fluids, 324.
Principle, the copying, 86.
of the cantilever, 291.
Printing machines, 305.
processes, 632.
telegraphs, 570.
Process blocks, 629.
Progress of mankind, 2.
Projectiles, 166.
air’s resistance to, 175.
deviation, 199.
long range of Whitworth, 193.
speed of, measured, 659.
trajectory of, 174.
Propagation of sound, 668.
Prospecting, 361.
Proteus anguinus, 684.
the modern, 807.
Pseudoscope, 472.
Puddling furnace, 45.
Q.
Queensferry, 292.
Quick-firing guns, 206.
Hotchkiss, 208.
R.
Railways, 101.
Great Western, 106.
Metropolitan, 114.
Midland, 112.
London and Manchester, 102, 124.
London and Woolwich, 103.
Pacific, 116.
St. Gothard, 371.
Stockton and Darlington, 101.
Randt, the, 694.
Rangoon petroleum, 759.
Rays polarized, 401.
Réaumur’s steel, 67.
Recoil, 172.
Recording instruments, 653.
Red-short iron, 62.
Reflection of light, 388.
Reflection in water, 396.
total, 399.
Refraction, 397.
double, 399.
Regulators for electric lamps, 523.
Reiss, 583.
Reliance building, the, 78.
Resistance, electrical, 494.
Resonance, electric, 540, 541.
Retina, 456.
Reverberatory furnace, 45.
Rifle, Brunswick, 180.
Chassepot, 182.
Lebel, 188.
Lee, 188.
Mannlicher, 187, 189.
Martini-Henry, 184.
magazine, 187.
Mauser, 187, 188.
military, 178.
Minié, 181.
Snider Enfield, 184.
Vetterli, 189.
Whitworth, 182.
Rifled cannon, 190.
Rifles, breech-loading, 182.
reduction of bores of, 189.
Rifling, 171.
guns, 199.
Righi, Professor, 541.
Rigi, railways ascending the, 126.
River steamboats, 144.
Rock boring, 349.
“Rocket,” 14.
Rock-drilling machines, 355.
Roentgen’s X-Rays, 445.
Rolling iron, 71.
Ronald’s telegraph, 548.
Roscoe, 425, 437, 444.
and Bunsen, 664.
Royal Gun Factory, Woolwich, 27.
Ruete’s ophthalmoscope, 466.
Ruhmkorff’s coil, 730.
S.
Saint Paul building, the, 78.
Saltash Bridge, 283.
San Francisco, 123.
Sand, properties of, 253.
Saw, circular, 100.
Sawing machines, 98.
Saturn, 440.
Schilling’s telegraph, 549.
Science, benefits of, 1.
Science and useful arts, 2.
Scott’s phonautograph, 669.
Screw, 86.
cutting lathe, 87.
dies and taps, 86.
propeller, 116, 117.
Sea anemones, 679.
horses, 684.
Secondary batteries, 530.
Segment shells, 217.
Senefelder, 556, 636.
Shear steel, 54.
Ship canals, 249.
Ships of war, 149.
Shrapnel shells, 216.
Siemens, 67.
Siemens’ dynamo, 522.
pneumatic tubes, 341.
regenerator, 68.
regulator, 523.
Siemens-Martin steel, 70.
Sight, 452.
Signals, railway, 108.
Silver plating by electricity, 500.
Sirius, 440.
Skerryvore, 596.
Skiagraphs, 447.
Slide rest, 87.
Smokeless powder, 226.
Snider rifle, 184.
Snow-plough, 123.
Sodium, 715.
Some phenomena of light, 382.
Sommeiller perforators, 351.
Sömmering’s telegraph, 548.
Sound, 665.
waves, 640.
Sounding telegraph, 566.
South African diamond fields, 701.
Speaking machine, 670.
tubes, 730.
Spectra, absorption, 431.
bright lined, 425.
of permanent gases, 430.
of salts, 424.
of stars, 440.
spark, 420.
Spectroscope, 416.
Spectrum, continuous, 422.
lithium, 425, 436.
of sodium, 424.
pure, 420.
solar, 418, 613.
Sphygmograph, 660.
Spiegeleisen, 64.
Stage illusions, 395.
Stars, distance of, 440.
motion of, 442.
spectra of, 440.
Steamboats, river, 144.
Steam engine, agricultural, 23.
domestic, 25.
forms of, 14.
Newcomen’s, 3.
portable, 23.
Watt’s double-acting, 6.
Watt’s improvements, 4.
Steam Engines, 3.
Steam carriages, 21.
expansive working of, 8, 17.
Steam hammer, the, 25.
Steam Navigation, 129.
Steam fire engine, 23.
“navvies,” 269.
rollers, 23.
superheated, 9.
Steamships, comparative sizes of, 138.
recent improvements in, 139.
speed of, 137.
Steel, 52, 68.
Bessemer, 58.
blister, 54.
cast, 54.
Krupp’s, 55.
puddled, 55.
Réaumur’s, 67.
shear, 54.
Siemens, 68.
tempering, 53.
tensile strength of, 53.
Stellar evolution, 810.
Stephenson, George, 14, 102.
Robert, 280.
St. Gothard Railway, 371.
Stereoscope, Brewster’s refracting, 470.
Wheatstone’s reflecting, 469.
Stereoscopic effect, 469.
lustre, 472.
views, 470, 474.
Stereotyping, 642.
Stevenson, Alan, 602, 603.
Storage battery, 530.
Strada, 547.
Stratified discharge, 517.
Stroboscopic disc, 476.
Submarine cables, 575.
Sub-Wealden, exploration, 362.
Suez Canal, the, 251.
Sun, elements in, 438.
constitution, 438.
Superheated steam, 9.
Surface plates, Whitworth’s, 94.
Suspension bridges, 284.
Sympathetic needles, 547.
Syphon recorder, 571.
Swan’s carbon process, 619, 481.
T.
Table, air resistance to projectile, 177.
benzol and toluol compounds, 789.
coal raised in Great Britain, 727.
coal-tar, colours, 794.
composition of cast iron, 43.
dimensions of parts of eye, 462.
dimensions of steamships, 138.
electric light v. gas, 514, 515.
floatation of Monarch and Captain, 160.
formulæ of hydro-carbon, 758.
Gramme machine, 518.
hydro-carbon in coal-tar, 782.
illuminating power of gases, 774.
lighthouse apparatus, 602.
lighthouse lamps, 597.
Martini-Henry rifle, 187.
nitrogen and oxygen compounds, 731, 732.
photographic actions, 612.
products from 100 lbs. of coal, 796.
ships of war, 166.
telegraph code, Morse’s, 560.
telegraph, war department, 557.
tenacities of iron, 277.
wave-lengths of colours, 411.
Wheatstone’s dot signals, 565.
Talbot, 610.
Talbotype, 611.
Tall buildings, 76.
Tawell, arrest of, 550.
Telegraphic instruments, 553.
Telegraphic lines, 572.
Telegraph poles, 572.
Telegraphs in Great Britain, 574.
Telegraphy, wireless, 546.
Tel-el-Kebir, 260.
Telepherage, 549.
Telephone, the, 581.
Telestereoscope, 472.
Tenacities of iron, 277.
Tension of electricity, 498.
Tesla, Nikola, 542.
oscillator, 542.
Tesla’s experiments, 545.
Thallium, 426.
The Terrible, H.M.S., 167.
Thomson, Sir W., 483, 570, 571, 805, 808.
Throttle valve, 6.
Thunderer, H.M.S., 164.
“Times” newspaper, 387, 312.
Tools, 85.
Torpedo-boats, 231.
Torpedoes, 227.
Tour de Cordouan, 593.
Tourmaline, 404.
Tower Bridge, the, 297.
Trajectory of projectile, 174.
Tramways, 22.
Transfer process, 638.
Transvaal, 693.
Tunnel, Mont Cenis, 351.
St. Gothard, 373.
Tunnels, helicoidal, 379.
Turbine, 144.
Turbinia, the, 144.
Turret ships, 154.
Tyndall, 383, 803, 809.
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.
ebookbell.com