Statistical Analysis: an Introduction using R/R/R as a calculator

R topic: R as a calculator

Text marked like this is used to discuss an R-specific 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 copy-and-paste them directly into your R session. Try the following to see how to use R as a simple calculator
1 100+2/3 Result:
> 100+2/3
 100.6667
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  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.
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:
1 #this is a comment: R will ignore it
2 (100+2)/3    #You can use round brackets to group operations so that they are carried out first
3 5*10^2       #The symbol * means multiply, and ^ means "to the power", so this gives 5 times (10 squared), i.e. 500
4 1/0          #R knows about infinity (and minus infinity)
5 0/0          #undefined results take the value NaN ("not a number")
6 (0i-9)^(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
 34
> 5*10^2       #The symbol * means multiply, and ^ means "to the power", so this is 5 times (10 squared)
 500
> 1/0          #R knows about infinity (and minus infinity)
 Inf
> 0/0          #undefined results take the value NaN ("not a number")
 NaN
> (0i-9)^(1/2) #for the mathematically inclined, you can force R to use complex numbers
 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