Читать книгу Probability with R - Jane M. Horgan - Страница 19

1.4 Basics

Оглавление

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.

Probability with R

Подняться наверх