0

I converted some columns from my dataset into factor levels to conduct analysis. How can I view the definitions of the factor levels which now inhabit my matrix? Are the original names lost? I used the following command on dtafactor, which is a matrix object.

dtafactor[,4:9]=factor(dtafactor[,4:9])
Info5ek
  • 1,227
  • 4
  • 17
  • 25
  • 1
    `levels(dtafactor[,4:9])`? – Vlo Jun 08 '15 at 17:36
  • > levels(dtafactor[,5:9])..... NULL – Info5ek Jun 08 '15 at 17:38
  • `str(dtafactor[,4:9])` results in what? – Pierre L Jun 08 '15 at 17:41
  • Is this what OP needs http://stackoverflow.com/questions/1195826/drop-factor-levels-in-a-subsetted-data-frame? – Pierre L Jun 08 '15 at 17:47
  • 1
    What is the `class()` of the matrix (a matrix can only hold a single atomic type)? You can't (easily) store factors in a matrix. It would really help if you created a fully [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MrFlick Jun 08 '15 at 17:57

1 Answers1

1

Assuming you really have a matrix, the information is lost. The reason is that a matrix can't hold a mixture of variables and can't hold a factor variable. Thus the integers that are the basis of factors (together with the levels attribute) are coerced to a type that fits with the rest of the columns (and the levels are lost), probably to character.

mat <- matrix(letters[1:4], 2)
mat[,2] <- factor(mat[,2])
#    [,1] [,2]
#[1,] "a"  "1" 
#[2,] "b"  "2"

You have to rerun your script up to that point. You probably should use a data.frame instead of a matrix as your data structure.

Roland
  • 127,288
  • 10
  • 191
  • 288