Book contents
- Frontmatter
- Contents
- Preface to the second edition
- Preface to the first edition
- 1 Getting started
- 2 Introduction to the R language
- 3 Programming statistical graphics
- 4 Programming with R
- 5 Simulation
- 6 Computational linear algebra
- 7 Numerical optimization
- Appendix Review of random variables and distributions
- Index
2 - Introduction to the R language
Published online by Cambridge University Press: 05 July 2016
- Frontmatter
- Contents
- Preface to the second edition
- Preface to the first edition
- 1 Getting started
- 2 Introduction to the R language
- 3 Programming statistical graphics
- 4 Programming with R
- 5 Simulation
- 6 Computational linear algebra
- 7 Numerical optimization
- Appendix Review of random variables and distributions
- Index
Summary
Having installed the R and RStudio systems, you are now ready to begin to learn the art of statistical programming. The first step is to learn the syntax of the language that you will be programming in; you need to know the rules of the language. This chapter will give you an introduction to the syntax of R. Most of what we discuss here relates to what you would type into the R console or into the RStudio script window.
First steps
Having opened R or RStudio, you may begin entering and executing commands, usually interactively. Normally, you will use the Source Pane to type in your commands, but you may occasionally use the Console Pane directly. The greater-than sign (>) is the prompt symbol which appears in the Console Pane.
R can be used as a calculator
Anything that can be computed on a pocket calculator can be computed at the R prompt. The basic operations are + (add), - (subtract), * (multiply), and / (divide). For example, try
5504982/131071
Upon pressing the Enter key (or CTRL-Enter), the result of the above division operation, 42, appears in the Console Pane, preceded by the command you executed, and prefixed by the number 1 in square brackets:
5504982/131071
## [1] 42
The [1] indicates that this is the first (and in this case only) result from the command. Many commands return multiple values, and each line of results will be labeled to aid the user in deciphering the output. For example, the sequence of integers from 17 to 58 may be displayed as follows:
17:58
## [1] 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
## [23] 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
The first line starts with the first return value, so it is labeled [1]; the second line starts with the 23rd, so it is labeled [23].
Everything that you type after a # sign is assumed to be a comment and is ignored by R.
- Type
- Chapter
- Information
- A First Course in Statistical Programming with R , pp. 7 - 48Publisher: Cambridge University PressPrint publication year: 2016