3 + 5
[1] 8
In this workshop we’ll use R in the extremely useful RStudio software. For the most part we’ll work interactively, meaning we’ll type stuff straight into the R console in RStudio (Usually this is a window on the left or lower left) and get our results there too (usually in the console or in a window on the right).
Panels like the ones below mimic the interaction with R and first show the thing to type into R, and below the calculated result from R.
Let’s look at how R works by using it for it’s most basic job - as a calculator:
3 + 5
[1] 8
12 * 2
[1] 24
1 / 3
[1] 0.3333333
12 * 2
[1] 24
Fairly straightforward, we type in the expression and we get a result. That’s how this whole book will work, you type the stuff in, and get answers out. It’ll be easiest to learn if you go ahead and copy the examples one by one. Try to resist the urge to use copy and paste. Typing longhand really encourages you to look at what you’re entering.
As far as the R output itself goes, it’s really straightforward - its just the answer with a [1]
stuck on the front. This [1]
tells us how many items through the output we are. Often R will return long lists of numbers and it can be helpful to have this extra information.
We can save the output of operations for later use by giving it a name using the assignment symbol <-
. Read this symbol as ‘gets’, so x <- 5
reads as ‘x gets 5’. These names are called variables, because the value they are associated with can change.
Let’s give five a name, x
then refer to the value 5 by it’s name. We can then use the name in place of the value. In the jargon of computing we say we are assigning a value to a variable.
<- 5
x x
[1] 5
* 2 x
[1] 10
<- 3
y * y x
[1] 15
This is of course of limited value with just numbers but is of great value when we have large datasets, as the whole thing can be referred to by the variable.
At the top level, R is a simple language with two types of thing: functions and objects. As a user you will use functions to do stuff, and get back objects as an answer. Functions are easy to spot, they are a name followed by a pair of brackets. A function like mean()
is the function for calculating a mean. The options (or arguments) for the function go inside the brackets:
sqrt(16)
[1] 4
Often the result from a function will be more complicated than a simple number object, often it will be a vector (simple list), like from the rnorm()
function that returns lists of random numbers
rnorm(100)
[1] 0.732897757 1.284273849 0.187198869 -1.034243713 -1.210191653
[6] 0.529005627 0.032822162 -1.041973338 0.132966823 -0.386635961
[11] -0.876308020 -0.752281620 -1.173259454 1.244074285 1.866598190
[16] -1.439940403 0.782396610 2.041652158 2.760626130 -1.748177099
[21] 0.780466701 -0.650389746 1.344092097 0.979555598 0.665452952
[26] 2.084609817 1.060719505 0.921245533 0.623392552 -0.438508369
[31] 0.003045055 0.482742819 -1.625884666 1.416898225 0.049944672
[36] -1.531147498 -0.466551548 -2.230926776 -0.754107702 1.144560128
[41] -1.030213004 -0.590831923 -0.972011803 -0.591506929 0.624087089
[46] -1.033646117 0.426749985 0.424718471 -0.103223218 -0.735826413
[51] -0.229487406 0.758234802 0.793415736 -0.806586054 0.192014961
[56] 0.107816854 -0.397479709 0.163718459 0.681293212 0.386425816
[61] 1.204251194 -0.930337719 -0.077239893 1.591728587 2.111400651
[66] -0.443678564 -0.214883181 1.068760404 1.829147961 -1.342124997
[71] 1.133437552 -1.694725778 0.632609984 0.178828164 -0.279367088
[76] 0.170304240 1.379341712 -1.576169828 1.298837278 3.000938683
[81] -0.407578563 0.885600067 -0.356960246 -1.086019919 -0.212972813
[86] 1.567942310 -1.468809170 0.046291557 -0.172891095 -0.489953624
[91] 0.400655882 -0.709094024 1.285519686 0.847435324 -1.554517576
[96] -1.418347252 -2.019989059 -0.160130496 -1.083778151 -0.200162877
We can combine objects, variables and functions to do more complex stuff in R, here’s how we get the mean of 100 random numbers.
<- rnorm(100)
numbers mean(numbers)
[1] -0.01382273
Here we created a vector object with rnorm(100)
and assigned it to the variable numbers
. We than used the mean()
function, passing it the variable numbers
. The mean()
function returned the mean of the hundred random numbers.
One of the more common objects that R uses is a dataframe. The dataframe is a rectangular table-like object that contains data, think of it like a spreadsheet tab. Like the spreadsheet, the dataframe has rows and columns, the columns have names and the different columns can have different types of data in. Here’s a little one
names age score
1 Guido 24 14.82414
2 Marty 45 18.67472
3 Alan 11 82.38981
Usually we get a dataframe by loading in data from an external source or as a result from functions, occasionally we’ll want to hand make one, which can be done with various functions, data.frame
being the most common.
data.frame(
names = c("Guido", "Marty", "Alan"),
age = c(24,45,11),
score = runif(3) * 100
)
Many of the tools we use in will come in R packages, little nuggets of code that group related functions together. Installing new packages can be done using the Packages
pane of RStudio or the install.packages()
function. When we wish to use that code we use the library()
function
library(somepackage)
R provides a command, called ?
that will display the documentation for functions. For example ?mean
will display the help for the mean()
function.
?mean
As in all programming languages the internal documentation in R is written with some assumption that the reader is familiar with the language. This can be a pain when you are starting out as the help will seem a bit obscure at times. Don’t worry about this, usually the Examples
section will give you a good idea of how to use the function and as your experience grows then the more things will make more sense.
* R is an excellent and powerful statistical computing environment
Complete the interactive tutorial online https://danmaclean.shinyapps.io/r-start