table()
   get_help() docs


Description

The table() function comes with R and is part of the Base R {base} package.

This function summarizes counts in categorical variables to quickly make a contingency table. All NAs in the data will be ignored.

Conceptual Usage

table(categorical array)

table(categorical array, another categorical array)

Examples

The examples below use the msleep dataset. Learn more about this dataset with get_help("msleep").

# Show the msleep dataset with head()
head(msleep)
## # A tibble: 6 × 11
##   name  genus vore  order conservation sleep_total sleep_rem sleep_cycle awake  brainwt  bodywt
##   <chr> <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>    <dbl>   <dbl>
## 1 Owl … Aotus omni  Prim… <NA>                17         1.8      NA       7    0.0155    0.48 
## 2 Moun… Aplo… herbi Rode… nt                  14.4       2.4      NA       9.6 NA         1.35 
## 3 Grea… Blar… omni  Sori… lc                  14.9       2.3       0.133   9.1  0.00029   0.019
## 4 Cow   Bos   herbi Arti… domesticated         4         0.7       0.667  20    0.423   600    
## 5 Thre… Brad… herbi Pilo… <NA>                14.4       2.2       0.767   9.6 NA         3.85 
## 6 Nort… Call… carni Carn… vu                   8.7       1.4       0.383  15.3 NA        20.5


# Tabulate msleep `vore`s (which itself is a categorical column)"
table(msleep$vore)
## 
##   carni   herbi insecti    omni 
##      10      24       4      18


# Tabulate msleep `vore`s and `order`s together, with `vore` in rows and `order` in columns
table(msleep$vore, msleep$order)
##          
##           Afrosoricida Artiodactyla Carnivora Cetacea Chiroptera Cingulata Didelphimorphia
##   carni              0            0         7       1          0         1               1
##   herbi              0            4         0       0          0         0               0
##   insecti            0            0         0       0          2         1               0
##   omni               1            1         0       0          0         0               1
##          
##           Diprotodontia Erinaceomorpha Hyracoidea Lagomorpha Perissodactyla Pilosa Primates
##   carni               0              0          0          0              0      0        0
##   herbi               1              0          2          1              3      1        1
##   insecti             0              0          0          0              0      0        0
##   omni                0              1          0          0              0      0        9
##          
##           Rodentia Scandentia Soricomorpha
##   carni          0          0            0
##   herbi         11          0            0
##   insecti        0          0            1
##   omni           1          1            3


# Tabulate msleep `vore`s and `order`s together, but with `order` in rows and `vore` in columns
table(msleep$order, msleep$vore)
##                  
##                   carni herbi insecti omni
##   Afrosoricida        0     0       0    1
##   Artiodactyla        0     4       0    1
##   Carnivora           7     0       0    0
##   Cetacea             1     0       0    0
##   Chiroptera          0     0       2    0
##   Cingulata           1     0       1    0
##   Didelphimorphia     1     0       0    1
##   Diprotodontia       0     1       0    0
##   Erinaceomorpha      0     0       0    1
##   Hyracoidea          0     2       0    0
##   Lagomorpha          0     1       0    0
##   Perissodactyla      0     3       0    0
##   Pilosa              0     1       0    0
##   Primates            0     1       0    9
##   Rodentia            0    11       0    1
##   Scandentia          0     0       0    1
##   Soricomorpha        0     0       1    3