Data is the result of measurement of physical or emotive quantities. We encode quantities like distance, clock cycles, rates of change, or feelings into measures. We then record repeated measures as data, which is grouped and labelled on a computer as variables.
How those quantities are encoded into measures impacts how we can analyze data.
It is often useful when analyzing data to think of the data as being one of four main types, according to the typology proposed by Stevens.1 This can help with selecting the analysis to perform and prevent basic analysis mistakes.
Some of the appropriate statistics and mathematical operations for each type are summarized in the table below.
| Scale Type | Statistics | Operations |
|---|---|---|
| Nominal | counts, mode, frequency, chi-squared, cluster analysis | \(=\), \(\neq\) |
| Ordinal | the above, plus: median, non-parametric tests, Kruskal-Wallis, rank-correlation | \(=\), \(\neq\), \(<\), \(>\) |
| Interval | plus: arithmetic mean, some parametric tests (proportions, sometimes t-test), individuals control chart, correlation, regression, ANOVA (sometimes), factor analysis | \(=\), \(\neq\), \(<\), \(>\) , \(+\), \(-\) |
| Ratio | plus: geometric and harmonic mean, ANOVA, correlation coefficient | \(=\), \(\neq\), \(<\), \(>\) , \(+\), \(-\) , \(\times\), \(\div\) |
While this is a useful typology for most use, and certainly for initial consideration, there are criticisms of Stevens’ typology, and there are alternatives. It doesn’t work as a hard-and-fast rule for all situations, so it is essential for the data scientist to understand the statistical methods being applied and their sensitivity to departures from underlying assumptions.
For example, percentages and count data have some characteristics of ratio-scale data, but with additional constraints. e.g. the average of the counts (2, 2, 1) may not be meaningful: can we talk meaningfully about \(1.66\ldots\) counts of something? Conversely, preference data collected on a Likert-type ordinal scale (e.g. on a scale of “not important” to “extremely important”) is clearly of ordinal type but may, in some cases, be safely treated as ratio scale data.
See Velleman and Wilkinson2 for one such critique of Stevens’ typology, in particular focusing on the way in which it fails when using automated machine learning methods.
R recognizes at least fifteen different types of data. Several of these are related to identifying functions and other objects—most users don’t need to worry about most of them. The main types that data scientists will need to use are:
x <- 1 + i2 or x <- complex(real=1, imaginary=2). Like type numeric, may be used for all scales of measurement.
TRUE or FALSE, typically used as nominal data.
The above types of data can be stored in several types, or structures, of variables. The equivalent to a variable in Excel would be rows, columns or tables of data. The main ones that we will use are:
c(1, 2, 1, 3, 4) is, by default, a numeric vector of type double, but c(1, 2, 1, 3, 4, "name") will be a character vector, or a vector where all data is stored as type character. In the latter case, the numbers will be stored as characters (i.e. on a nominal scale of measurement) rather than as numbers.
factor(c("a", "b", "c", "a"), levels=c("a","b","c","d"))
# letters a - c in 2x4 array
array(data=letters[1:3], dim=c(2,4))
# numbers 1 - 3 in 2x4 array
array(data=1:3, dim=c(2,4))
matrix(data = rep(1:3, times=2), nrow=2, ncol=3)
list("text", "more", 2, c(1,2,3,2))
NROW()). However, unlike matrices, different columns can contain different types of data and each row and column must have a name. If not named explicitly, R automatically names rows by their row number and columns according to the data assigned assigned to the column. Data frames are typically used to store the sort of data that industrial engineers and scientists most often work with, and is the closest analog in R to an Excel spreadsheet. Usually data frames are made up of one or more columns of factors and one or more columns of numeric data.
data.frame(rnorm(5), rnorm(5), rnorm(5))
More generally, in R all variables are objects, and R distinguishes between objects by their internal storage type and by their class declaration, which are accessible via the functions typeof()and class(). Functions in R are also objects, and the users can define new objects to control the output from functions. This is how summary() and print() are able to work with a wide range of object types, from numeric vectors to data frames to ggplot2 graph objects. For more on objects, types and classes, see section 2 of the R Language Definition.