Calling elements in a data frame

Ok, now that you know how to store data in R. We need to learn how to see the data. To see a given data container, you can simply typeb its name and click enter. For instance,

DataFrame
##   Height Width
## 1      1    56
## 2      4    42
## 3      5    93

This is an ok way to see your data if the data container is not too big. However, if you have lots of data this command will fill up your screen. Instead, you can use the function head or tail, which will let you see the top five or the bottom five rows in a dataframe. Lets see.

Let me first create a medium size dataframe:

DataFrame<- data.frame( x1 = c(rep(1,250)),     # in Column 1 I repeat the number 1 for 25 times
                        x2 = seq(1:250), #Column 2 I create a sequence of numbers from 1 to 25
                        x3 = sample(seq(1:1000),250)) # select 25 random numbers between 1 and 10000

In the code above, I just create a dummy dataframe with three columns and 250 rows using functions we already described in the chapter on arithmetic operators.

If I try to see the full database, I just type the name of the dataframe and click enter. However, as you will notice calling the full dataframe will use a lot of your screen space because it will attempt to display all the data.

DataFrame
##     x1  x2  x3
## 1    1   1 576
## 2    1   2 107
## 3    1   3 523
## 4    1   4 219
## 5    1   5 561
## 6    1   6 436
## 7    1   7 907
## 8    1   8 901
## 9    1   9 118
## 10   1  10 234
## 11   1  11  17
## 12   1  12 804
## 13   1  13 241
## 14   1  14 214
## 15   1  15 635
## 16   1  16  79
## 17   1  17 746
## 18   1  18  22
## 19   1  19 582
## 20   1  20 714
## 21   1  21 311
## 22   1  22   2
## 23   1  23 202
## 24   1  24 412
## 25   1  25 237
## 26   1  26 225
## 27   1  27 231
## 28   1  28 312
## 29   1  29 602
## 30   1  30 805
## 31   1  31 918
## 32   1  32 251
## 33   1  33 788
## 34   1  34 472
## 35   1  35 416
## 36   1  36 276
## 37   1  37 852
## 38   1  38 922
## 39   1  39 629
## 40   1  40 462
## 41   1  41 513
## 42   1  42 460
## 43   1  43 893
## 44   1  44 327
## 45   1  45 406
## 46   1  46 844
## 47   1  47 308
## 48   1  48  62
## 49   1  49 280
## 50   1  50 801
## 51   1  51 876
## 52   1  52  51
## 53   1  53 180
## 54   1  54 908
## 55   1  55 324
## 56   1  56 969
## 57   1  57 563
## 58   1  58 622
## 59   1  59 826
## 60   1  60 823
## 61   1  61 295
## 62   1  62  13
## 63   1  63 160
## 64   1  64 309
## 65   1  65 940
## 66   1  66 205
## 67   1  67 993
## 68   1  68 980
## 69   1  69 967
## 70   1  70 293
## 71   1  71 521
## 72   1  72 592
## 73   1  73 278
## 74   1  74  35
## 75   1  75 919
## 76   1  76 591
## 77   1  77 304
## 78   1  78 464
## 79   1  79  57
## 80   1  80 479
## 81   1  81 745
## 82   1  82 124
## 83   1  83 385
## 84   1  84 665
## 85   1  85 781
## 86   1  86 372
## 87   1  87 664
## 88   1  88 628
## 89   1  89 699
## 90   1  90 413
## 91   1  91 758
## 92   1  92 297
## 93   1  93 121
## 94   1  94 507
## 95   1  95 615
## 96   1  96 619
## 97   1  97 282
## 98   1  98 150
## 99   1  99 477
## 100  1 100 465
## 101  1 101 483
## 102  1 102   7
## 103  1 103 425
## 104  1 104 206
## 105  1 105  61
## 106  1 106 910
## 107  1 107 458
## 108  1 108  98
## 109  1 109 807
## 110  1 110   4
## 111  1 111 904
## 112  1 112 709
## 113  1 113 997
## 114  1 114 287
## 115  1 115 760
## 116  1 116 504
## 117  1 117 199
## 118  1 118 981
## 119  1 119 983
## 120  1 120 755
## 121  1 121 752
## 122  1 122 842
## 123  1 123 923
## 124  1 124   6
## 125  1 125 537
## 126  1 126 725
## 127  1 127  78
## 128  1 128 572
## 129  1 129 135
## 130  1 130 947
## 131  1 131 579
## 132  1 132 498
## 133  1 133 400
## 134  1 134 728
## 135  1 135 870
## 136  1 136 496
## 137  1 137 393
## 138  1 138 176
## 139  1 139 850
## 140  1 140 122
## 141  1 141  89
## 142  1 142 864
## 143  1 143 396
## 144  1 144 832
## 145  1 145 175
## 146  1 146 147
## 147  1 147 672
## 148  1 148 900
## 149  1 149 444
## 150  1 150 336
## 151  1 151 973
## 152  1 152  29
## 153  1 153  72
## 154  1 154 429
## 155  1 155 426
## 156  1 156 588
## 157  1 157 866
## 158  1 158 965
## 159  1 159 570
## 160  1 160 512
## 161  1 161 232
## 162  1 162 443
## 163  1 163 789
## 164  1 164  18
## 165  1 165 666
## 166  1 166 226
## 167  1 167 773
## 168  1 168 151
## 169  1 169 146
## 170  1 170 169
## 171  1 171 608
## 172  1 172 371
## 173  1 173 256
## 174  1 174  16
## 175  1 175 571
## 176  1 176 391
## 177  1 177 687
## 178  1 178 949
## 179  1 179  66
## 180  1 180 648
## 181  1 181  58
## 182  1 182 542
## 183  1 183 851
## 184  1 184 761
## 185  1 185 155
## 186  1 186 364
## 187  1 187 475
## 188  1 188 459
## 189  1 189 794
## 190  1 190 201
## 191  1 191  34
## 192  1 192 772
## 193  1 193 538
## 194  1 194 285
## 195  1 195 430
## 196  1 196 777
## 197  1 197 813
## 198  1 198 317
## 199  1 199 208
## 200  1 200 333
## 201  1 201  60
## 202  1 202 711
## 203  1 203 693
## 204  1 204 621
## 205  1 205 656
## 206  1 206 380
## 207  1 207 222
## 208  1 208 356
## 209  1 209 438
## 210  1 210  37
## 211  1 211 808
## 212  1 212 281
## 213  1 213 657
## 214  1 214 623
## 215  1 215 669
## 216  1 216 379
## 217  1 217  95
## 218  1 218 244
## 219  1 219 955
## 220  1 220 677
## 221  1 221 741
## 222  1 222 649
## 223  1 223 793
## 224  1 224 499
## 225  1 225 954
## 226  1 226 812
## 227  1 227 529
## 228  1 228  27
## 229  1 229 586
## 230  1 230 536
## 231  1 231  46
## 232  1 232 583
## 233  1 233 266
## 234  1 234 590
## 235  1 235 593
## 236  1 236 702
## 237  1 237 618
## 238  1 238 976
## 239  1 239 283
## 240  1 240 344
## 241  1 241 612
## 242  1 242 601
## 243  1 243 553
## 244  1 244 585
## 245  1 245 948
## 246  1 246 726
## 247  1 247 470
## 248  1 248 694
## 249  1 249 105
## 250  1 250 787

Tail

Or the bottom rows using the tail function. Like this:

tail(DataFrame)
##     x1  x2  x3
## 245  1 245 948
## 246  1 246 726
## 247  1 247 470
## 248  1 248 694
## 249  1 249 105
## 250  1 250 787

Index

You can also check specific elements of the dataframe using the index function, which in R is indicated with the square brackets [row,column]. The number to the left of the comma will be the row number, and the number to the right the column number. If you do not add a number, it will display all columns or all rows. For instance, check what number is in column 3 in the 2th row?

DataFrame[2,3]
## [1] 107

Calling columns by name

To call a column by name in a dataframe, you use the dollar sign $ to merge the name of the dataframe with the name of the column, like this,

head(DataFrame$x3) #here I only display the top values of the column Country_Name
## [1] 576 107 523 219 561 436