Probability with R. Jane M. Horgan

Probability with R - Jane M. Horgan


Скачать книгу
alt="images"/>, 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

      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)

      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"

      which are all of the objects that contain the word “boxplot.”

      Before carrying out a statistical analysis, it is necessary to get the data into the computer. How you do this varies depending on the amount of data involved.

      1.6.1 Reading and Displaying Data on Screen

      A small data set, for example, a small set of repeated measurements on a single variable, may be entered directly from the screen. It is usually stored as a vector, which is essentially a list of numbers.

      Example 1.1 Entering data from the screen to a vector

      The total downtime occurring in the last month of 23 workstations in a computer laboratory was observed (in minutes) as follows:

      To input these data from the screen environment of R, write

      downtime <- c(0, 1, 2, 12, 12, 14, 18, 21, 21, 23, 24, 25, 28, 29, 30, 30, 30, 33, 36, 44, 45, 47, 51)

is used to define a vector containing the 23 data points. These data are then assigned to a vector called downtime.

      To view the contents of the vector, type

      downtime

      which will display all the values in the vector

.

      R handles a vector as a single object. Calculations can be done with vectors like ordinary numbers provided they are the same length.

      1.6.2 Reading Data from a File to a Data Frame

      When the data set is large, it is better to set up a text file to store the data than to enter them directly from the screen.

      A large data set is usually stored as a matrix, which consists of columns and rows. The columns denote the variables, while the rows are the observations on the variables. In R, this type of data set is stored in what is referred to as a data frame.

      Definition 1.1 Data frame

      A data frame is an object with rows and columns or equivalently it is a list of vectors of the same length. Each vector consists of repeated observations of some variable. The variables may be numbers, strings or factors.

      Example 1.2 Reading data from a file into a data frame

      The examination results for a class of 119 students pursuing a computing degree are given on our companion website (www.wiley.com/go/Horgan/probabilitywithr2e) as a text file called

. The complete data set is also given in Appendix A.

       gender arch1 prog1 arch2 prog2 m 99 98 83 94 m NA NA 86 77 m 97 97 92 93 m 99 97 95 96 m 89 92 86 94 m 91 97 91 97 m 100 88 96 85 f 86 82 89 87 m 89 88 65 84 m 85 90 83 85 m 50 91 84 93 m 96 71 56 83 f 98 80 81 94 m 96 76 59 84 ....


Скачать книгу