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,
## 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 10000In 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.
## x1 x2 x3
## 1 1 1 662
## 2 1 2 37
## 3 1 3 735
## 4 1 4 845
## 5 1 5 388
## 6 1 6 448
## 7 1 7 532
## 8 1 8 431
## 9 1 9 630
## 10 1 10 440
## 11 1 11 207
## 12 1 12 13
## 13 1 13 143
## 14 1 14 883
## 15 1 15 580
## 16 1 16 405
## 17 1 17 494
## 18 1 18 59
## 19 1 19 301
## 20 1 20 582
## 21 1 21 988
## 22 1 22 3
## 23 1 23 854
## 24 1 24 646
## 25 1 25 374
## 26 1 26 471
## 27 1 27 855
## 28 1 28 652
## 29 1 29 338
## 30 1 30 91
## 31 1 31 77
## 32 1 32 710
## 33 1 33 476
## 34 1 34 366
## 35 1 35 34
## 36 1 36 335
## 37 1 37 663
## 38 1 38 684
## 39 1 39 542
## 40 1 40 136
## 41 1 41 184
## 42 1 42 908
## 43 1 43 126
## 44 1 44 680
## 45 1 45 273
## 46 1 46 223
## 47 1 47 601
## 48 1 48 997
## 49 1 49 253
## 50 1 50 407
## 51 1 51 35
## 52 1 52 871
## 53 1 53 584
## 54 1 54 905
## 55 1 55 347
## 56 1 56 177
## 57 1 57 304
## 58 1 58 368
## 59 1 59 133
## 60 1 60 478
## 61 1 61 746
## 62 1 62 308
## 63 1 63 861
## 64 1 64 282
## 65 1 65 812
## 66 1 66 176
## 67 1 67 288
## 68 1 68 63
## 69 1 69 846
## 70 1 70 89
## 71 1 71 459
## 72 1 72 624
## 73 1 73 560
## 74 1 74 594
## 75 1 75 454
## 76 1 76 122
## 77 1 77 647
## 78 1 78 161
## 79 1 79 422
## 80 1 80 255
## 81 1 81 890
## 82 1 82 824
## 83 1 83 536
## 84 1 84 275
## 85 1 85 944
## 86 1 86 119
## 87 1 87 481
## 88 1 88 515
## 89 1 89 586
## 90 1 90 959
## 91 1 91 749
## 92 1 92 151
## 93 1 93 1
## 94 1 94 423
## 95 1 95 915
## 96 1 96 419
## 97 1 97 72
## 98 1 98 941
## 99 1 99 960
## 100 1 100 191
## 101 1 101 187
## 102 1 102 274
## 103 1 103 113
## 104 1 104 638
## 105 1 105 767
## 106 1 106 186
## 107 1 107 634
## 108 1 108 399
## 109 1 109 290
## 110 1 110 682
## 111 1 111 450
## 112 1 112 864
## 113 1 113 750
## 114 1 114 396
## 115 1 115 467
## 116 1 116 29
## 117 1 117 641
## 118 1 118 943
## 119 1 119 98
## 120 1 120 805
## 121 1 121 610
## 122 1 122 493
## 123 1 123 107
## 124 1 124 775
## 125 1 125 649
## 126 1 126 676
## 127 1 127 28
## 128 1 128 392
## 129 1 129 199
## 130 1 130 548
## 131 1 131 480
## 132 1 132 679
## 133 1 133 952
## 134 1 134 842
## 135 1 135 539
## 136 1 136 376
## 137 1 137 528
## 138 1 138 578
## 139 1 139 613
## 140 1 140 499
## 141 1 141 390
## 142 1 142 828
## 143 1 143 800
## 144 1 144 298
## 145 1 145 579
## 146 1 146 120
## 147 1 147 698
## 148 1 148 64
## 149 1 149 181
## 150 1 150 555
## 151 1 151 554
## 152 1 152 123
## 153 1 153 219
## 154 1 154 153
## 155 1 155 605
## 156 1 156 625
## 157 1 157 642
## 158 1 158 355
## 159 1 159 776
## 160 1 160 397
## 161 1 161 203
## 162 1 162 445
## 163 1 163 278
## 164 1 164 533
## 165 1 165 484
## 166 1 166 218
## 167 1 167 881
## 168 1 168 395
## 169 1 169 463
## 170 1 170 189
## 171 1 171 53
## 172 1 172 628
## 173 1 173 869
## 174 1 174 421
## 175 1 175 695
## 176 1 176 820
## 177 1 177 629
## 178 1 178 782
## 179 1 179 54
## 180 1 180 1000
## 181 1 181 316
## 182 1 182 97
## 183 1 183 787
## 184 1 184 957
## 185 1 185 777
## 186 1 186 728
## 187 1 187 330
## 188 1 188 299
## 189 1 189 4
## 190 1 190 671
## 191 1 191 398
## 192 1 192 975
## 193 1 193 331
## 194 1 194 974
## 195 1 195 156
## 196 1 196 759
## 197 1 197 928
## 198 1 198 164
## 199 1 199 453
## 200 1 200 778
## 201 1 201 149
## 202 1 202 310
## 203 1 203 734
## 204 1 204 124
## 205 1 205 939
## 206 1 206 384
## 207 1 207 474
## 208 1 208 748
## 209 1 209 797
## 210 1 210 356
## 211 1 211 590
## 212 1 212 517
## 213 1 213 69
## 214 1 214 627
## 215 1 215 173
## 216 1 216 902
## 217 1 217 251
## 218 1 218 898
## 219 1 219 317
## 220 1 220 596
## 221 1 221 142
## 222 1 222 626
## 223 1 223 130
## 224 1 224 980
## 225 1 225 867
## 226 1 226 874
## 227 1 227 92
## 228 1 228 834
## 229 1 229 426
## 230 1 230 323
## 231 1 231 134
## 232 1 232 955
## 233 1 233 632
## 234 1 234 847
## 235 1 235 519
## 236 1 236 276
## 237 1 237 435
## 238 1 238 966
## 239 1 239 530
## 240 1 240 502
## 241 1 241 501
## 242 1 242 155
## 243 1 243 325
## 244 1 244 721
## 245 1 245 438
## 246 1 246 730
## 247 1 247 313
## 248 1 248 449
## 249 1 249 811
## 250 1 250 333
Head
Alternatively, you can just check the top rows using the head function. Like this:
## x1 x2 x3
## 1 1 1 662
## 2 1 2 37
## 3 1 3 735
## 4 1 4 845
## 5 1 5 388
## 6 1 6 448
Tail
Or the bottom rows using the tail function. Like this:
## x1 x2 x3
## 245 1 245 438
## 246 1 246 730
## 247 1 247 313
## 248 1 248 449
## 249 1 249 811
## 250 1 250 333
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?
## [1] 37