Chapter 1 CoopR
The first contact with R
might feel a little overwhelming, especially if you haven’t done any programming before. Some things are fairly easy to understand but others do not come naturally to people. To help you with the latter group, here are some tips on how to approach talking to computers.
Talking to a computer is like talking to Sheldon Cooper (of The Big Bang Theory fame): he is a highly proficient user of the English language: he understands the vocab, grammar, and syntax perfectly well. Since he is very smart, he can do many complex things you ask him to do, so long as you are formal, logical, precise, and unambiguous. If you are not, he won’t be able to help you or worse, will do something wrong. When it comes to the pragmatics of English, he’s a bit lost: he doesn’t understand metaphors, shorthand, or sarcasm. If you want to tell jokes, you’d be better off talking to someone else.
Keep this analogy in mind whenever you’re working with R
. Talk to it as you would talk to Sheldon: don’t take anything for granted, don’t rely on R
to make any assumptions about what you are telling it. Certainly don’t try to be funny and expect R
to appreciate it! It takes a little practice to get into the habit of translating what you want to do into the dry, formal language of logic and algorithms but, on the flip side, once you get the knack of it, there will never be any misunderstanding on the part of the machine. With people, there will always be miscommunication, no matter how clear you try to be, because people are very sensitive to paralinguistic cues, non-verbal communication, and the pragmatics of a language. Computers don’t care about any of that and thus, if they’re not doing what you want them to, the problem is in the sender, not the receiver.1 That is exactly why they’re built like that!
Instead of thinking about computers as scary, abstruse, or boring, learn to appreciate this quality of precision as beautiful. That way, you’ll get much more enjoyment out of working with them.
1.1 Think algorithmically
By algorithmically, we mean in a precise step-by-step fashion where each action is equivalent to the smallest possible increment. So, if you want a machine to make you a cup of tea, instead of saying: “Oi CoopR
! Fetch me a cuppa!”, you need to break everything down. Something like this:
Go to kitchen
If no kitchen, jump to 53
Else get kettle
If no kettle, jump to 53
Check how much water in kettle
If is more or equal than 500 ml of water in kettle, jump to 15
Place kettle under cold tap
Open kettle lid
Turn cold tap on
Check how much water in kettle
If there is more than 500 ml of water in kettle, jump to 13
Jump to 10
Turn cold tap off
Close kettle lid
If kettle is on base, jump to 17
Place kettle on base
If base is plugged in mains, jump to 19
Plug base in mains
Turn kettle on
Get cup
If no cup, jump to 53
Get tea bag
# yeah, we’re not poshIf no tea bag, jump to 53
Put tea bag in cup
If kettle has boiled, jump to 27
Jump to 25
Start pouring water from kettle to cup
Check how much water in cup
If there is more than 225 ml of water in cup, jump to 31
Jump to 28
Stop pouring water from kettle
Return kettle on base
Wait 4 minutes
Remove tea bag from cup
Place tea bag by edge of sink
# this is obviously your flatmate’s programOpen fridge
Search fridge
If full fat milk is not in fridge, jump to 41
Get full fat milk
# full fat should be any sensible person’s top choice. True fact!Jump to 43
If semi-skimmed milk is not in fridge, jump to 51
# skimmed is not even an option!Get semi-skimmed milk
Take lid of milk
Start pouring milk in cup
Check how much liquid in cup
If more or equal to 250 ml liquid in cup, jump to 48
Jump to 45
Stop pouring milk
Put lid on milk
Return milk to fridge
Close fridge
# yes, we kept it open! Fewer steps (I’m not lazy, I’m optimised!)Bring cup
# no need to stirEND
… so there you go, a nice cuppa in 53 easy steps!
Obviously, this is not real code but something called pseudocode: a program-like set of instructions expressed in a more-or-less natural language. Surely, we could get even more detailed, e.g., by specifying what kind of tea we want, but 53 steps is plenty. Notice, however, that, just like in actual code, the entire process is expressed in a sequential manner. This sometimes means that you need to think about how to say things that would be very easy to communicate to a person (e.g., “Wait till the kettle boils”) in terms of well-defined repeatable steps. In our example, this is done with a loop:
If kettle has boiled, jump to 27
Jump to 25
Start pouring water from kettle to cup
The program will check if the kettle has boiled and, if it has, it will start pouring water into the cup. If it hasn’t boiled, it will jump back to 25. and check again if it has boiled. This is equivalent to saying “wait till the kettle boils”.
Note that in actual R
code, certain expressions, such as jump to
(or, more frequently in some older programming languages goto
) are not used so you would have to find other ways around the problem, were you to write a tea-making program in R
. However, this example illustrates the sort of thinking mode you need to get into when talking to computers. Especially when it comes to data processing, no matter what it is you want your computer to do, you need to be able to convey it in a sequence of simple steps.
1.2 Don’t be scared
It is quite normal to be a little over-cautious when you first start working with a programming language. Let us assure you that, when it comes to R
, there is very little chance you’ll break your computer or damage your data, unless you set out to do so and know what you’re doing. When you read in your data, R
will copy it to the computer’s memory and only work with this copy. The original data file will remain untouched. So please, be adventurous, try things out, experiment, break things! It’s the best way to learn programming and understand the principles and the logic behind the code.
An important insight immortalised by Newton Crosby in the 1986 cult movie Short Circuit: “It’s a machine, Schroeder. It doesn’t get pissed off. It doesn’t get happy, it doesn’t get sad, it doesn’t laugh at your jokes. It just runs programs.”↩