This chapter introduces R and describes some of its basic operations and editing procedures. The syntax used to read and edit statistical data and to perform some basic statistical calculations is given.
It is not the intention to provide a complete set of features of the language, but rather to give a flavor of the structure of R and how to get help to proceed further. Additional features are introduced in later chapters as the need for them arises.
R is a data‐analysis system, a sophisticated calculator and an object‐oriented programming language. It provides an environment for statistical analysis and graphics that differs from standard statistical packages such as SPSS and Minitab; these provide point‐and‐click graphical‐user interfaces (GUIs), while R is command‐driven. Users type commands at a prompt, and the R interpreter responds.
It is possible to get quite far using R to execute simple expressions from the command line, and some users may never need to go beyond this. At a more advanced level, users write their own functions, either to systematize repetitive work or to develop add‐on packages for new functionality.
R is obtained from the website called CRAN (Comprehensive R Archive Network) and is downloaded by proceeding as follows:
Go to the CRAN website at http://cran.r-project.org/;
Choose an operating system from Linux, (Mac) OS X, and Windows appropriate to your computer. In this book, we work in the Windows environment, click “Download R for Windows”;
Choose the “base” package;
Click “Download R 3.6.1”, which is the current version at the time of writing.
Press the option “Run.”
R is now installed, and you should see an “R” icon on your computer. Clicking on this will start up the standard R package.
R documentation is available at http://cran.r-project.org/manuals, where you will find the following manuals:
An Introduction to R gives an introduction to the language and how to use R for doing statistical analysis and graphics;
The R Language Definition documents the language per se, that is, the objects that it works on, and the details of the expression evaluation process, which are useful to know when programming R functions;
Writing R Extensions covers how to create your own packages, how to write R help files, and the foreign language (C, C++, Fortran, etc.) interfaces;
R Data Import/Export describes the import and export facilities available either in R itself or via packages that are available from CRAN;
R Installation and Administration;
R Internals is a guide to the internal structures of R and coding standards for the core team working on R itself;
The R Reference Index contains all help files of the R standard and recommended packages in printable form.
The documentation may be downloaded or browsed. We suggest that you download and obtain a hard copy of An Introduction to R by Venables et al. (2019) Version 3.6.1 (2019-07-05) and access the others as you require.
To familiarize yourself with R , you should work through the commands given below at a workstation to which R has been downloaded.
To start, either click on the R icon (if you have created a short cut on your screen) or go to “Programs,” select R , and then click on the R icon. When the R program is started, and after it prints an introductory message on the screen, the interpreter prompts for input with the command prompt “
.”
Expressions that are typed at the command prompt are evaluated and printed. For example,
6+7*3/2
returns
[1] 16.5
To assign or store data, write
x <- 1:4
Here, the integers 1, 2, 3, 4 are assigned to the vector
. To check the contents of
, type
x
which returns
[1] 1 2 3 4
To square the elements of
, write
x2 <- x**2
or equivalently
x2 <- x^2
that causes each element in the vector
to be squared and stored in the vector
. To examine the contents of
, write
x2
which gives
[1] 1 4 9 16
To illustrate how R is case sensitive, consider
X <- 10 prod1 <- X*x prod1 [1] 10 20 30 40
Here, the integer 10 is stored in
.
causes each element of the vector
to be multiplied by 10.
Some points to note:
<���‐ is the assignment operator; in the illustration “ <���‐ 1: 4, the integers are assigned to the vector ;
R is case sensitive; for example, and represent different variables;
Variable names can consist of any combination of lower and upper case letters, numerals, periods, and underscores, but cannot begin with a numeral or underscore;
All the above examples of variables are numeric, but we shall see that R supports many other types of data.
The entities that R creates and manipulates are called objects . These include variables, arrays of numbers, strings, or functions.
All objects created in R are stored in what is known as the workspace .
The easiest way of getting help when you are working in the R environment is to click the Help button on the toolbar.
Alternatively, you can type
help()
for online help, or
help.start()
for an HTML browser interface.
It could be helpful to look at some demonstrations of R by typing
demo()
which gives a list of all available demonstrations.
Demonstrations on specific topics can be obtained by inserting an argument. For example,
demo(plotmath)
gives some examples of the use of mathematical notation.
A more specific way of getting help, when working in the R environment, is to type the name of the function you require. For example,
help(read.table)
will provide details on the exact syntactic structure of the instruction read.table
.
An alternative is
?read.table
To obtain all that is available on a particular topic, use apropos
.
apropos ("boxplot")
returns
"boxplot", "boxplot.default", "boxplot.stats"
Читать дальше