6.3 Counting statistics

Next, we’ll move on to common counting functions for vectors with discrete or non-numeric data. Discrete data are those like gender, occupation, and monkey farts, that only allow for a finite (or at least, plausibly finite) set of responses. Common functions for discrete vectors are in Table 6.3. Each of these vectors takes a vector as an argument – however, unlike the previous functions we looked at, the arguments to these functions can be either numeric or character.

Table 6.3: Counting functions for discrete data.
Function Description Example Result
unique(x) Returns a vector of all unique values. unique(c(1, 1, 2, 10)) 1, 2, 10
table(x, exclude) Returns a table showing all the unique values as well as a count of each occurrence. To include a count of NA values, include the argument exclude = NULL table(c("a", "a", "b", "c")) 2-"a", 1-"b", 1-"c"

Let’s test these functions by starting with two vectors of discrete data:

vec <- c(1, 1, 1, 5, 1, 1, 10, 10, 10)
gender <- c("M", "M", "F", "F", "F", "M", "F", "M", "F")

The function unique(x) will tell you all the unique values in the vector, but won’t tell you anything about how often each value occurs.

unique(vec)
## [1]  1  5 10
unique(gender)
## [1] "M" "F"

The function table() does the same thing as unique(), but goes a step further in telling you how often each of the unique values occurs:

table(vec)
## vec
##  1  5 10 
##  5  1  3
table(gender)
## gender
## F M 
## 5 4

If you want to get a table of percentages instead of counts, you can just divide the result of the table() function by the sum of the result:

table(vec) / sum(table(vec))
## vec
##    1    5   10 
## 0.56 0.11 0.33
table(gender) / sum(table(gender))
## gender
##    F    M 
## 0.56 0.44