Page 47 - Applied Statistics Using SPSS, STATISTICA, MATLAB and R
P. 47

26       1 Introduction


              > x
              [1] 1 2 3 4 5 6

              The  [1]   means the first element of x . For instance,

              > y <- rnorm(12)
              > y
               [1] -0.1354 -0.2519  0.5716  0.6845 -1.5148 -0.1190
               [7]  0.7328 -1.0274  0.3319 -0.3468 -1.2619  0.7146

                                                                              st
           generates and lists a vector with 12 normally distributed random numbers. The 1
                th
           and 7  elements are indicated. (The numbers are represented here with four digits
           after the decimal point because of page width constraints. In R the representation is
           with seven  digits.)  One could also obtain the  previous list by just  issuing:  >
           rnorm(12)  . Most R functions also behave as procedures in that way, displaying
           lists of values in the R console.
              A vector can be filled with strings  (delimited with  “), as in  v <-
           c(“Pmax”, T80”, T82”). Now  v is  a vector containing three strings. The
                      “
                             “
           second vector element, v[2], is “T80”
              R also provides a function, named  seq  , to define evenly spaced number
           sequences, as in the following example:

              > seq(-1,1,0.2)
               [1] -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0

              A matrix can be obtained in R by suitably transforming a vector. For instance,

              > dim(x) <- c(2,3)
              > x
                   [,1] [,2] [,3]
              [1,]    1    3    5
              [2,]    2    4    6

           transforms (through the dim   function) the previous vector x  into a matrix of 2×3
           elements. Note the display of row and column numbers.
              One can also  aggregate vectors into a matrix  by using the function  c bind
           (“column binding”) or rbind (“row binding”) as in the following example:

              > u <- c(1,2,3)
              > v <- c(-1,-2,-3)
              > m <- cbind(u,v)
              > m
                   u  v
              [1,] 1 -1
              [2,] 2 -2
              [3,] 3 -3

              Matrix indexing in R uses square brackets as index qualifier. As an example,
           m[2,2]   has the value -2  .
              Note that R  is case-sensitive.  For instance,  Cbind   cannot be used  as  a
           replacement for  cbind  .
   42   43   44   45   46   47   48   49   50   51   52