Search code examples
rdata-representation

Matrix representation of categories in R


I have a column with a set of categories e.g. Category1, Category2, Category3,Category2, etc. Is there any way to represent them in R as a matrix with the following view:

Category1 Category2 Category3
        1         0         0
        0         1         0
        0         0         1
        0         1         0
        ...       ...       ...

Any feedback is greatly appreciated.


Solution

  • I would do the following:

    matrixForm <- function(X) {
       values <- sort(unique(X))
       outM <- matrix(0, nrow=length(X), ncol=length(values))
       for (i in 1:ncol(outM)) outM[,i] <- as.numeric(X==values[i])
       colnames(outM) <- values
       return(outM)
    }
    

    X is an input vector of characters. So the result...

    X <- c('Category1','Category2','Category2','Category3','Category1','Category3')
    matrixForm(X)
    #################
         Category1 Category2 Category3
    [1,]         1         0         0
    [2,]         0         1         0
    [3,]         0         1         0
    [4,]         0         0         1
    [5,]         1         0         0
    [6,]         0         0         1