Page 44 - Applied Statistics with R
P. 44

44                         CHAPTER 3. DATA AND PROGRAMMING


                                 example_data


                                 ##    x        y     z
                                 ## 1  1    Hello  TRUE
                                 ## 2  3    Hello FALSE
                                 ## 3  5    Hello  TRUE
                                 ## 4  7    Hello FALSE
                                 ## 5  9    Hello  TRUE
                                 ## 6  1    Hello FALSE
                                 ## 7  3    Hello  TRUE
                                 ## 8  5    Hello FALSE
                                 ## 9  7    Hello  TRUE
                                 ## 10 9 Goodbye FALSE

                                 Unlike a list which has more flexibility, the elements of a data frame must all
                                 be vectors, and have the same length.
                                 example_data$x


                                 ##  [1] 1 3 5 7 9 1 3 5 7 9

                                 all.equal(length(example_data$x),
                                            length(example_data$y),
                                            length(example_data$z))


                                 ## [1] TRUE

                                 str(example_data)


                                 ## 'data.frame':     10 obs. of   3 variables:
                                 ##  $ x: num   1 3 5 7 9 1 3 5 7 9
                                 ##  $ y: chr   "Hello" "Hello" "Hello" "Hello" ...
                                 ##  $ z: logi   TRUE FALSE TRUE FALSE TRUE FALSE ...
                                 nrow(example_data)


                                 ## [1] 10

                                 ncol(example_data)


                                 ## [1] 3
   39   40   41   42   43   44   45   46   47   48   49