0

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.

talat
  • 68,970
  • 21
  • 126
  • 157
Ivan T
  • 1,046
  • 1
  • 10
  • 22
  • 1
    In this case, either `model.matrix(~ x -1)` or `diag(nlevels(factor(x)))[factor(x),]` would be the best I reckon. – thelatemail Mar 17 '15 at 22:37

1 Answers1

0

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
Bridgeburners
  • 637
  • 5
  • 15
  • 1
    @IvanT - I'd take a look at the duplicate question first. There are built-in methods in R for this operation that won't require looping and pre-assigning. – thelatemail Mar 17 '15 at 22:38