Suppose, that we have a categorical variable say color which can take one of three values: Red, Green and Yellow.

We can code then using dummy coding or effects coding. But, suppose, we were to code it as follows:

Red:     1 
Green:   2 
Yellow:  3

Leaving aside issues of estimation and interpretation, is there a name for coding categorical variables as above?

  • 1
    $\begingroup$ Ordinal? I can't see why you would ever want to do this, unless you have a very strong belief that $R<G<Y$ (or $>$) and that is is exactly linear. $\endgroup$
    – Affine
    Jul 14, 2014 at 20:26
  • 2
    $\begingroup$ This is not a coding scheme because it is unable to distinguish clearly among the colors. For instance, it would equate the average of Red and Yellow with Green. It amounts to a kind of (arbitrary) valuation of the colors. Therefore it does not have any standard name. $\endgroup$
    – whuber
    Jul 14, 2014 at 20:56
  • $\begingroup$ @whuber 'It does not have any standard name.' Perhaps, add your comment as an answer? $\endgroup$
    – user32139
    Jul 14, 2014 at 21:36

1 Answer 1


That would fall under the category of numerical coding.

In fact, take a look at this piece of R code:

colors <- c("Red", "Green", "Yellow")
colors.df <- data.frame(color=sample(colors, size=25, replace=TRUE))

If you run these lines you'll see that R actually internally encodes factors in the very same way you've described. Crazy!

As suggested in the Comments, there is one important caveat, however: Just because you're using numbers to efficiently represent the values of your categorical variable does not mean that you can treat them as numbers. In other words, your data remains categorical regardless of any name changes you may have applied.

Nevertheless, there's nothing inherently wrong with this particular coding scheme. In fact, it's entirely reasonable.

Hope this helps!

SEE ALSO: The numerical coding of information (Swerdlin, 1974)


Today, it seems as though (at least to me) that the concept of coding arises most often in the context of regression (for example, as mentioned in the original post, recoding categorical variables as dummy variables). However, the notion of coding systems is actually much more general than this current usage may suggest, as indicated by its entry in Dover's Outline of Basic Statistics: Dictionary and Formulas:

Coding System - In information theory, any consistent scheme used to represent
  a given set of data. A coding system is usually employed either to reduce 
  error or increase efficiency in the transmission of information.

According to this standard, it is clear that the simple mapping of colors to integers as described in the original post would, indeed, qualify as a coding scheme. Other than merely reducing the size of the data being stored (since, for example, the vector c("Green", "Green", "Yellow", "Red") could now be stored more compactly as c(2,2,3,1)), there does not appear to be much benefit to this specific scheme, however. Nevertheless, it still qualifies as a coding system.

Now, to elaborate on the "important caveat" mentioned above (in my original answer), this particular coding scheme is very misleading within the regression setting. In fact, in its entry for "Dummy Variable", the Cambridge Dictionary of Statistics indicates the importance of re-coding (via Dummy Variables) such numerical codes when in the regression setting:

"Such recoding is used before polychotomous variables are used as
explanatory variables in a regression analysis to avoid the unreasonable
assumption that the original numerical codes for the categories, i.e. the
values 1,2,...,k, correspond to an interval scale."

Thus, it's not that this system of coding is meaningless, per se, within the regression setting; rather, the misspecification is due to attributing more meaning to these numerical codes than would otherwise be warranted. Make sense?

Hope this clarifies my original answer!

  • $\begingroup$ You are confusing encoding with naming of factors. The encoding for a trivariate factor requires two-dimensional vectors and this is what R uses under the hood. It definitely does not use the numbers $1,2,3$ to encode these three strings. $\endgroup$
    – whuber
    Jul 15, 2014 at 16:39
  • $\begingroup$ From CRAN: Factors are currently implemented using an integer array to specify the actual levels and a second array of names that are mapped to the integers. It's the coding system represented here--the mapping of the names to integers--that I am referring to. $\endgroup$
    – Steve S
    Jul 15, 2014 at 22:44
  • $\begingroup$ I believe you misunderstand the meaning of "integer array." It does not refer to the array c(1,2,3) as your answer suggests. It instead refers to using a vector for each distinct factor. These vectors appear within the rows of the model matrix. You can see them with x <- factor(c("red","green","yellow")); model.matrix(~x). The factor names are "1", "2", and "3" while their encodings are $(1,1,0)$, $(1,0,0)$, and $(1,0,1)$, respectively. The map is from names to vectors rather than integers. The closest Swerdlin gets to this is his fertilizer example which distinguishes N, P, and K. $\endgroup$
    – whuber
    Jul 16, 2014 at 0:27
  • $\begingroup$ No, that's just it--by creating a design matrix you are actually re-coding the coded numerical data in order to make it suitable for regression. As described in the Cambridge Dictionary of Statistics: "Such recoding is used before *polychotomous* variables are used as explanatory variables in a regression analysis to avoid the unreasonable assumption that the original numerical codes for the categories, i.e. the values 1,2,...,k, correspond to an *interval* scale." $\endgroup$
    – Steve S
    Jul 16, 2014 at 9:55
  • $\begingroup$ In other words, you have the categories (e.g. "Red", "Green", "Yellow"), you have the original numerical codes for the categories (e.g. 1,2,3), and then (assuming you want to be able to run a regression) you re-code with the k-1 dummy variables, etc. $\endgroup$
    – Steve S
    Jul 16, 2014 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.