R programming MCA() in FactoMineR error message

Jingjing Ni picture Jingjing Ni · Nov 3, 2015 · Viewed 7.1k times · Source

I was using the MCA() function from FactoMineR package in R to do the multiple correspondence analysis on a set of around 160 variables with around 2000 observations. Around 150 of the variables are continuous, so I first used the cut() function to convert those continuous variables to categorical variables and then used MCA() function.

My code is very simple like this:

library(FactoMineR)

data<-read.csv('demographics.csv')

for (i in 9:length(data)){

   temp<-unlist(data[i],use.names=FALSE)

   data[i]<-cut(temp,breaks=5,labels=c('A','B','C','D','E'))
}

MC<-MCA(data,ncp=10,graph=TRUE)

After I run the code, I got the following error message.

Error in dimnames(res) <- list(attributes(tab)$row.names, listModa) : length of 'dimnames' [2] not equal to array extent

I am wondering why this error occurs and how to fix it. There is no missing data in my table and all of the variables are categorical.

If anyone has encountered similar problems and would love to help, I would really appreciate it. Thanks a lot.

Answer

KRS picture KRS · Nov 18, 2015

I have had this error before because the function requires the variables to be factors (and the data I was passing it wasn't fully converted into factors). Unlike a lot of other R functions, this one does not convert the data for you even if all columns are categorical.

I'm not quite sure what your data is, but it is likely that one or more columns is not as a factor variable. If your columns 1 to 8 are already factors then it may be in the read.csv call; string variables will automatically be converted into a factors when you read them in from the csv, but numeric ones will not.