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.
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:
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.
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:
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: