Primer 1 · Objects, functions and vectors
R in your browser, nothing to install
This page runs R inside your browser. The first load downloads about 17 MB: R itself, plus the machinery that runs your code. On a slow connection that takes a minute or two.
If nothing happens after that, your network is blocking it. Work through the R primer in RStudio instead. It teaches the same material.
There is no Stop button. If you write a loop that never ends, reload the page.
Three ideas carry most of the R in this course. R stores things in objects, you do things with functions, and the commonest object is a vector. Run the cells, change the numbers, and try things that might not work.
Objects
<- is the assignment arrow. It puts the thing on its right into the name on its left.
Nothing printed when you assigned. R stored the value silently. Typing the name on its own line is what prints it.
Change 2400 to a number from your own work and run it again.
You will see = used for assignment in other people’s code. It mostly works. Use <- anyway: it is the convention, and = means something different inside a function call, which is exactly where beginners get caught.
Functions
A function is a verb. It takes arguments in brackets, separated by commas, and gives back a value.
round(3.14159, 2) passed 2 arguments: the number, and how many decimal places. Most confusion about R functions is confusion about which argument is which.
You can name arguments, and then the order stops mattering:
Vectors
A vector is several values of the same type in a row. c() combines them. The c is for combine.
Functions applied to a vector work on every element at once. There is no loop to write.
Arithmetic works the same way. This is vectorisation, and it is why R code for data work is usually short.
Six divisions, one line, no loop.
Types
A vector holds one type. The 3 you will meet most often:
Mix them and R quietly converts everything to the most forgiving type, usually character. This is a common source of “why is my column text?”.
Look at what happened: 14 became "14", with quotation marks. You can no longer take its mean.
Comparison, and why logicals matter
Comparing a vector gives you a logical vector, one answer per element.
And a logical vector sums as if TRUE were 1 and FALSE were 0. So sum() counts, and mean() gives a proportion.
That second line is prevalence in miniature: the share of a group who have something. Primer 6 does it properly.
Missing values
Real data has holes. R writes a hole as NA, and it is contagious on purpose: the mean of a set of numbers you do not fully know is not knowable.
You get NA, not an error. R is telling you the answer depends on the 2 values you do not have.
To average only the ones you do have, say so:
na.rm = TRUE is a decision, not a fix
It changes the question. You asked for the mean of 7 people. You now have the mean of the 5 you measured.
Sometimes that is fine. Sometimes the 2 missing people are missing because they were the sickest. Then it is not fine. Ask why the values are missing before you drop them.
What you should now be able to do
- Store a value in a name with
<-, and print it by typing the name. - Call a function with positional or named arguments.
- Build a vector with
c(), and apply a function to all of it at once. - Recognise the three common types, and why mixing them turns numbers to text.
- Count and take proportions with
sum()andmean()over a comparison. - See
NAfor what it is, and decide about it deliberately.