Preface

“Introduction to Programming with R is a learning resource for programming novices who want to learn programming using the statistical programming language R. While one of the major strengths of R is the broad variety of packages for statistics and data science, this resource focuses on learning and understanding basic programming concepts using base R. Only a couple of additional packages are used and/or briefly discussed for special tasks.

This online book is specifically written for participants of the course “Introduction to Programming: Programming in R” offered by the Digital Science Center at Universität Innsbruck. If you are interested in further literature you may consider other resources:

A big thank you

… goes to all who helped writing and improving this book. Whether it was a full chapter, a section, or drawing attention to errors, typos, or incomprehensible wording.

As for now, this learning resource has to be seen as ‘work in progress’ and there are, for sure, many more typos and transposed letters which will be fixed in the (near) future.

Output styling

The book comes with a series of differently styled code blocks. Blue blocks show R source code or user input, the commands we write as users. The result or return of the commands when executed is typically shown directly below in gray.

# Command/user input
x <- "Default style: all fine."
print(x)
## [1] "Default style: all fine."

Besides the standard style shown above, messages, warnings and errors are shown differently when the occur. While messages (not critical) are shown in green, warnings and errors (should be addressed) are shown in yellow/orange and red.

## When messages are produced by R they will be shown in green.
## Warning: Warnings thrown are highlighted in yellow/orange.
## Error: Errors result in reddish output.

Bad examples of source code (how it should not be done) are shown with a red vertical bar on the left hand side.

x <- -35
if(x<0){    print("negative value")
            } else{print(x)
}
## [1] "negative value"

Practical exercises

Every now and then you will find “practical exercises” within the chapters indicated by an orange question mark. By clicking on the title the exercise will show up (open). The exercises come with solutions (indicated by an orange light bulb). By default, the solution is hidden (closed). Try to solve the exercise on your own before reading the solution!

Exercise 0.1 A hand is an old non-SI unit to measure the size of a horse. An Australien friend just called us and told us he bought a new horse “[…] which is 12.5 hands tall.”. One hand is 101.6 millimeters. How tall is his horse in meters?

Solution. Well, that’s a very simple question, but the solution would be something like:

# Calculate the size in meters
x <- 12.5 * 101.6 / 1000 # / 1000 to scale to meters
# Simple print
x
## [1] 1.27
# Or use "cat" to create the output
cat("His horse is ", x, "meters tall") 
## His horse is  1.27 meters tall

Advanced topics

Advanced blocks contain content which goes beyond the “main topic” of this course. Feel free to skip these parts if you think they may be too difficult for you at the moment.

Sometimes you will find blocks which look like this one. These blocks indicate advanced topics (or the beginning of a section labeled as “advanced”).

Excursions

Occasionally you will find “excursion” entries. They are not mandatory but show additional hints/examples.

Not real exercises, nor advanced topics, just small excursions from the current topic or additional hints/details about specific functions or methods.