# The R Student Companion

## Preview

## Book Description

R is the amazing, free, open-access software package for scientific graphs and calculations used by scientists worldwide. **The R Student Companion** is a student-oriented manual describing how to use R in high school and college science and mathematics courses. Written for beginners in scientific computation, the book assumes the reader has just some high school algebra and has no computer programming background.

The author presents applications drawn from all sciences and social sciences and includes the most often used features of R in an appendix. In addition, each chapter provides a set of computational challenges: exercises in R calculations that are designed to be performed alone or in groups.

Several of the chapters explore algebra concepts that are highly useful in scientific applications, such as quadratic equations, systems of linear equations, trigonometric functions, and exponential functions. Each chapter provides an instructional review of the algebra concept, followed by a hands-on guide to performing calculations and graphing in R.

R is intuitive, even fun. Fantastic, publication-quality graphs of data, equations, or both can be produced with little effort. By integrating mathematical computation and scientific illustration early in a student’s development, R use can enhance one's understanding of even the most difficult scientific concepts. While R has gained a strong reputation as a package for statistical analysis, **The R Student Companion** approaches R more completely as a comprehensive tool for scientific computing and graphing.

## Table of Contents

**Introduction: Getting Started with R**R Tutorial

Vectors

Graphs

Real-World Example

Final Remarks

Computational Challenges

References

**R Scripts**

Creating and Saving an R Script

Running an R Script

Finding Errors in an R Script

Sharpening Up Your Scripts with Comments

Real-World Example

Final Remarks

Computational Challenges

Reference

**Functions**

Creating New Functions in R

More about User-Defined R Functions

Real-World Example

Final Remarks

Computational Challenges

Afternotes (Short Remarks on Topics for Further Study)

References

**Basic Graphs**

Real-World Example

Graphs of One Variable

Graphs of Two Variables

Final Remarks

Computational Challenges

Afternotes

**Data Input and Output**

Data Frames in R

Final Remarks

Computational Challenges

Afternotes

**Loops**

Writing a "For-Loop"

Checking the Loop

OK, Mr. Fibonacci…So What?

Real-World Example

Final Remarks

Computational Challenges

References

**Logic and Control**

Logical Comparison Operators and Logical Vectors

Boolean Operations

Missing Data

More about Indexes

Conditional Statements

Real-World Example

Final Remarks

Computational Challenges

Afternotes

Reference

**Quadratic Functions**

Real-World Example

Final Remarks

Computational Challenges

References

**Trigonometric Functions**

Right Triangles

Trigonometric Functions

Right Triangles, Circles, and Radians

Properties of Trigonometric Functions

Polar Coordinates

Triangulation of Distances

Real-World Examples

Final Remarks

Computational Challenges

Afternotes

**Exponential and Logarithmic Functions**

Achieving Real Power

The Special Number e

The Number e in Applications

The Exponential Function

Exponential Growth

Logarithmic Functions

Logarithmic Scales

Real-World Examples

Final Remarks

Computational and Algebraic Challenges

References

**Matrix Arithmetic**

Another Way to Multiply Vectors

Matrix Multiplication

Matrix Addition and Subtraction

Reading a Data File into a Matrix

Real-World Example

Final Remarks

Computational Challenges

Afterwords

References

**Systems of Linear Equations**

Matrix Representation

Matrix Inverse

Inverse Matrices and System Solutions in R

Real-World Examples

Final Remarks

Computational Challenges

Afternotes

References

**Advanced Graphs**

Two-Dimensional Plots

Options for Styles of Symbols, Lines, Axes

Other Customizations

Multiple Panels

Three-Dimensional Plots

Color

Final Remarks

Computational Challenges

Reference

**Probability and Simulation**

Random Variables

Probability

Probability Distributions of Counts

Probability Distributions of Measurements

Real-World Example

Computational Challenges

Afternotes

References

**Fitting Models to Data**

Fitting a Quadratic Model

Multiple Predictor Variables

Nonlinear Statistical Models

Final Remarks

Computational Challenges

Afternotes

References

**Conclusion—It Doesn’t Take a Rocket Scientist**

Real Solar System Example

The Problem

The Concept

Changes in Velocities

Move the Earth

Getting Organized

Outline of R Script for Calculating the Trajectory of Earth

The R Script

Computational Challenges

Afternotes

**Appendix A: Installing R**

**Appendix B: Getting Help**

**Appendix C: Common R Expressions**

**Index**

## Author(s)

### Biography

**Brian Dennis** is a professor with a joint appointment in the Department of Fish and Wildlife Sciences and the Department of Statistical Sciences at the University of Idaho. He received a master's degree in statistics and a Ph.D. in ecology from The Pennsylvania State University. He has authored over 70 scientific articles on applications of statistics and mathematical modeling in ecology and natural resource management. He has been enthusiastically using R in his scientific work and teaching R in his courses for a decade.

## Reviews

"… a good book for high school or college students wanting to learn R on their own. Complete mathematical explanations paired with computational examples in R provide an excellent tool for these students to obtain a solid foundation in R."

—Erin R. Leatherman,The American Statistician, November 2014"One of the positive features of this book is its fearlessness. The message is that you are never too young to learn R (the book is aimed at high school and college students) and that the best way to learn is to jump straight into writing R expressions. The author’s enthusiasm for tackling challenges head on also shines off the page. … I am also in favour of the hands-on approach of the book; it is clearly written to be read while sitting at a keyboard. … The problem-based style of the book is another plus, where learning is motivated by first setting up an interesting question to answer. … a lively and interesting introduction to R. It furnishes a lot of useful material for introducing a very powerful tool to enquiring young minds."

—Paul Murrell,Australian & New Zealand Journal of Statistics, 2014"This book requires no prior knowledge of calculus, programming, or statistics. … the commands and real-world examples are explained very thoroughly. This should make the book suitable for self-study and hold interest for the target group (high school and college-level students) …"

—Joonas Kauppinen,International Statistical Review(2013), 81, 2"An R book for high schoolers! This is an excellent idea, and the quality of the product is equally excellent. It may be suitable for non-calculus-based introductory courses at the college level as well. … Dennis does a good job dispelling the ‘steep learning curve’ myth concerning R … . The writing style is clear and lively, and the examples should appeal to high school students. It is high time that introductory statistics be taught in an engaging manner that reflects our own enthusiasm for the subject, with meaningful data sets, attractive graphics, and so on. Dennis’ book is a fine contribution toward that goal."

—Norman Matloff,Journal of Statistical Software, Volume 52, February 2013"What do you get out of Dennis’s book? You learn how to install R and add-on packages. You learn the basic syntax and discover how to make plots and do basic computations using R. A reader of this book will learn how to do some essential programming in R, and also see it applied to solve interesting example of real scientific problems. Indeed,

The R Student Companionmakes picking up R quick and easy."

—Jason Graham,MAA Reveiws