R base functions

head is a simple function that shows the first n values in an object, with default n = 6. Additionally, if you include a negative sign in front of your n integer, it will display the object without the last n items.

Examples

Show the output with the first five rows of the airports data frame

Click to see solution
myDF <- read.csv("/anvil/projects/tdm/data/flights/subset/airports.csv")
head(myDF)
    iata              airport                       city     state      country      lat          long
1   00M               Thigpen                Bay Springs        MS	    USA	    31.95376	 -89.23450
2   00R	           Livingston       Municipal Livingston        TX	    USA	    30.68586	 -95.01793
3   00V	          Meadow Lake           Colorado Springs        CO	    USA	    38.94575    -104.56989
4   01G	         Perry-Warsaw                      Perry        NY	    USA	    42.74135	 -78.05208
5   01J	     Hilliard Airpark                   Hilliard        FL	    USA	    30.68801	 -81.90594
6   01M	    Tishomingo country                   Belmont        MS	    USA	    34.49167	 -88.20111

table

table is a function used to build a contingency table, which is a table that shows counts for categorical data, from one or more categories. prop.table is a function that accepts table output, returning proportions of the counts.

Examples

Show how many airports are found in each state, first in alphabetical order (which is the default), and then in sorted order.

Click to see solution
# default
table(myDF$state)

# sorted
sort(table(myDF$state))
# default
 AK  AL  AR  AS  AZ  CA  CO  CQ  CT  DC  DE  FL  GA  GU  HI  IA  ID  IL  IN  KS
263  73  74   3  59 205  49   4  15   1   5 100  97   1  16  78  37  88  65  78
 KY  LA  MA  MD  ME  MI  MN  MO  MS  MT  NC  ND  NE  NH  NJ  NM  NV  NY  OH  OK
 50  55  30  18  34  94  89  74  72  71  72  52  73  14  35  51  32  97 100 102
 OR  PA  PR  RI  SC  SD  TN  TX  UT  VA  VI  VT  WA  WI  WV  WY
 57  71  11   6  52  57  70 209  35  47   5  13  65  84  24  32

 # numerically ordered
  DC  GU  AS  CQ  DE  VI  RI  PR  VT  NH  CT  HI  MD  WV  MA  NV  WY  ME  NJ  UT
  1   1   3   4   5   5   6  11  13  14  15  16  18  24  30  32  32  34  35  35
 ID  VA  CO  KY  NM  ND  SC  LA  OR  SD  AZ  IN  WA  TN  MT  PA  MS  NC  AL  NE
 37  47  49  50  51  52  52  55  57  57  59  65  65  70  71  71  72  72  73  73
 AR  MO  IA  KS  WI  IL  MN  MI  GA  NY  FL  OH  OK  CA  TX  AK
 74  74  78  78  84  88  89  94  97  97 100 100 102 205 209 263