R topic: R as a calculator
Text marked like this is used to discuss an Rspecific point. The basics of R can be learned by reading these sections in the order they appear in the book. There will also be commands that can be entered directly into R; you should be able to copyandpaste them directly into your R session^{[1]}. Try the following to see how to use R as a simple calculator
In the absence of any instructions of what to do with the output of a command, R usually prints the result to the screen. For the time being, ignore the [1] before the answer: we will see that this is useful when R outputs many numbers at once. Note that R respects the standard mathematical rules of carrying out multiplication and division before addition and subtraction: it divides 2 by 3 before adding 100.
Input:

100+2/3
Result:
> 100+2/3
[1] 100.6667
R commands can sometimes be rather difficult to follow, so occasionally it can be useful to annotate them with comments. This can be done by typing a hash (#) character: any further text on the same line is ignored by R. This will be used extensively in the R examples in this wikibook, e.g.
Input:

#this is a comment: R will ignore it

(100+2)/3 #You can use round brackets to group operations so that they are carried out first

5*10^2 #The symbol * means multiply, and ^ means "to the power", so this gives 5 times (10 squared), i.e. 500

1/0 #R knows about infinity (and minus infinity)

0/0 #undefined results take the value NaN ("not a number")

(0i9)^(1/2) #for the mathematically inclined, you can force R to use complex numbers
Result:
> #this is a comment: R will ignore it
> (100+2)/3 #You can use round brackets to group operations so that they are carried out first [1] 34 > 5*10^2 #The symbol * means multiply, and ^ means "to the power", so this is 5 times (10 squared) [1] 500 > 1/0 #R knows about infinity (and minus infinity) [1] Inf > 0/0 #undefined results take the value NaN ("not a number") [1] NaN > (0i9)^(1/2) #for the mathematically inclined, you can force R to use complex numbers [1] 0+3i
 If you don't know anything about complex numbers, don't worry: they are not important here.
 Note that you can't use curly brackets {} or square brackets [] to group operations together
NotesEdit
 ↑ Depending on how you are viewing this book, may see a ">" character in front of each command. This is not part of the command to type: it is produced by R itself to prompt you to type something. This character should be automatically omitted if you are copying and pasting from the online version of this book, but if you are reading the paper or pdf version, you should omit the ">" prompt when typing into R.