# Statistical Analysis: an Introduction using R/R basics
## Why R?

R is a command-driven statistical package. At first sight, this can make it rather daunting to use. However, there are a number of reasons to learn statistics using this computer program. The two most important are:
   * R is free; you can download it from http://www.r-project.org and install it onto just about any sort of computer you like.
   * R allows you to do all the statistical tests you are likely to need, from simple to highly advanced ones. This means that you should always be able to perform the right analysis on your data.
An additional bonus is that R has excellent graphics and programming capabilities, so can be used as an aid to teaching and learning. For example, all the illustrations in this book have been produced using R; by clicking on any illustration, you can obtain the R commands used to produce it.
A final benefit, which is of more use once you have some basic knowledge of either statistics or R, is that there are many online resources to help users of R. A list is available in the appendix to this book.

## How to use this book with R

The main text in this book describes the why and how of statistics, which is relevant whatever statistical package you use. However, alongside the main text, there are a large number of "R topics": exercises and examples that use R to illustrate particular points. You may find that it takes some time to get used to R, especially if you are unfamiliar with the idea of computer languages.
Don't worry! The topics in this chapter and in Chapter 2 should get you going, to the point where you can understand and use R's basic functionality. This chapter is intended to get you started: once you have installed R, there are topics on how to carry out simple calculations and use functions, how to store results, how to get help, and how to quit. The few exercises in Chapter 1 mainly show the possibilities open to you when using R, then Chapter 2 introduces the nuts and bolts of R usage: in particular vectors and factors, reading data into data frames, and plotting of various sorts. From then on, the exercises become more statistical in nature.
If you wish to work straight through these initial exercises before statistical discussion, they are collected here. Note that when working through R topics online, you may find it more visually appealing if you set up wikibooks to display R commands nicely. If the R topics get in the way of reading the main text, they can be hidden by clicking on the arrow at the top right of each box.

#### Starting R

If you don't already have R installed on your computer, download the latest version for free from http://www.r-project.org, and install the base system. You don't need to install any extra packages yet. Once you have installed it, start it up, and you should be presented with something like this:
You are now in an R session. R is a command-driven program, and the ominous-looking ">" character means that R is now waiting for you to type something. Don't be daunted. You will soon get the hang of the simplest commands, and that is all you should need for the moment. And you will eventually find that the command-line driven interface gives you a degree of freedom and power that is impossible to achieve using more "user-friendly" packages.

#### R as a calculator

[1] 100.6667
> (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 > (0i-9)^(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

#### Storing objects

> big.num <- 10 * 100 #You can put the name first if you reverse the arrow (set big.num to 10000). > big.num+small.num+1 #Now you can treat big.num and small.num as numbers, and use them in calculations [1] 1001.001 > my.result <- big.num+small.num+2 #And you can store the result of any calculation > my.result #To look at the stored object, just type its name [1] 1002.001 > pi #There are some named objects that R provides for you [1] 3.141593

#### Functions

Most arguments to a function are named. For example, the first argument of the citation function is named package. To provide extra clarity, when using a function you can provide arguments in the longer form name=value. Thus
does the same as
refers to a function, whereas
is a "string" of text. This is useful, for example when providing titles for plots, etc.

#### Quitting R
## Setting up wikibooks

Before you start on the main text, we recommend that you add a few specific wikibooks preferences. The first three lines will display the examples of R commands in a nicer format. The last line gives a nicer format to figures consisting of multiple plots (known as subfigures). You can do this by creating a user CSS file, as follows.
* Make sure you are logged in (and create yourself an account if you do not have one already).
* Visit your personal css stylesheet, at Special:MyPage/skin.css.
* Click on "Edit this page".
* Paste the following lines into the large edit box
* If you know any CSS, make any alterations you like to this stylesheet.
* Finally save the page by clicking on "Save page",
Enough! Let's move on to the main text.

## Notes

* ↑ These are poor attempts at a statistical jokes, as you will soon find out.
* ↑ 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.
* ↑ If you are familiar with computer programming languages, you may be used to using the underscore ("_") character in names. In R, "." is usually used in its place.
* ↑ you can use either single (') or double (") quotes to delimit text strings, as long as the start and end quotes match
* ↑ (note that this is a temporary hack until GeSHi supports R code, in which case Statistical Analysis: an Introduction using R/R/Syntax can be changed. The css code should really read.pre {padding:0; border: none; margin:0; line-height: 1.5em; } .source-R ol {list-style: none; font-size: 1.2em; margin-left: 0;} .source_R ol li div:before {content: "\003E \0020";}