[R] how to make the matrix as factors

Gabor Grothendieck ggrothendieck at myway.com
Sun Dec 19 14:41:10 CET 2004


Note that an easy way to get them all to be factors is to add the
appropriate colClasses= argument to your read.table call:

   leaf <- read.table(...whatever...,  colClasses = "factor")
   mca(leaf)

See ?read.table for more about the colClasses= argument.

Pierre BADY <pierre.bady <at> univ-lyon1.fr> writes:

: 
: Hi,
: 
: The argument df of the function Â'mcaÂ’ requires only a data.frame (not matrix)
: containing only factors.
: 
: # Example:
: ?data.frame
: 
: library(MASS)
: ?mca
: 
: # we  construct the matrix.
: size <- c(3,2,3,3,3,3,3,3,3)
: texture <- c(2,3,2,2,2,2,2,2,2)
: leaf <- cbind(size,texture)
: 
: new.mca <- mca(leaf)
: 
: # Error in mca(leaf) : All variables must be factors
: 
: # now, we construct a data.frame.
: dleaf <- as.data.frame(leaf)
: summary(dleaf)
: 
: # we transform each variable into a factor. 
: dleaf[,2] <- as.factor(dleaf[,2])
: dleaf[,1] <- as.factor(dleaf[,1])
: summary(dleaf)
: new.mca <- mca(dleaf)
: new.mca
: #
: 
: cheers,
: 
: P.BADY
: 
: 
: En réponse à ys03165003 <at> student.ecnu.edu.cn:
: 
: > Hi All,
: > 
: > Thanks for you help, I have loaded the library MASS to call the mca. But
: > wen I
: > want to do the mca, there is another problem, for example
: > --------
: > --------
: > > leaf <- read.table("C:/Documents and Settings/wxh-c/×ÃÃæ/1.txt",
: > + col.names=c("size","texture"),header=TRUE)
: > > leaf
: >     size texture
: > 1      3       3
: > 2      2       2
: > 3      3       2
: > 4      3       2
: > 5      3       2
: > 6      3       3
: > 7      3       2
: > 8      3       2
: > 9      3       2
: > > attach(leaf)
: > > require(MASS)
: > Loading required package: MASS 
: > [1] TRUE
: > > new.mca <- mca(leaf)
: > Error in mca(leaf) : All variables must be factors
: > ---------
: > So, what should I do to make the matrix as factor. Waiting for you
: > answers.
: > Thanks!
: > 
: >                                                                 jeff
: > 
: > ______________________________________________
: > R-help <at> stat.math.ethz.ch mailing list
: > https://stat.ethz.ch/mailman/listinfo/r-help
: > PLEASE do read the posting guide!
: > http://www.R-project.org/posting-guide.html
: > 
: > 
: 
: ---------------------------------------------------------
: Pierre BADY     <°)))><
: Université Claude Bernard Lyon 1
: UMR CNRS 5023, LEHF
: bat Alphonse Forel
: 43 boulevard du 11 novembre 1918 
: F-69622 VILLEURBANNE CEDEX 
: FRANCE
: TEL : +33 (0)4 72 44 62 34 
: FAX : +33 (0)4 72 43 28 92 
: MEL : pierre.bady <at> univ-lyon1.fr
: http://limnologie.univ-lyon1.fr
: 
: ______________________________________________
: R-help <at> stat.math.ethz.ch mailing list
: https://stat.ethz.ch/mailman/listinfo/r-help
: PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
: 
:




More information about the R-help mailing list