[R] Coercing character to factor

Marc Feldesman feldesmanm at pdx.edu
Wed Mar 8 21:30:13 CET 2000

I just downloaded version 1.0.0 and several binary libraries (VR, rpart, 
norm, stataread) - WinNT version.  I then converted a file from Stata 6.0 
to R format by using the stataread library.  The file converts perfectly 
and I was able to use the VR function lda on the dataframe without 
difficulty.  I then tried to use the same dataframe with RPART.  The model 

test.rp<-rpart(genus~x+y+z+a+b+c, data=mydata) fails with the following error:

Error in model.frame(formula, rownames, variables, varnames, extras, 
extranames,  :
         invalid variable type

(the identical model statement works perfectly in lda)

I've traced the error to how RPART (or R) deals with the dependent variable 
"genus", which is converted from a Stata file to an R file as a "character" 

The model statement works fine if I do:

test.rp<-rpart(as.factor(genus)~x+y+z+a+b+c, data=mydata)


test.rp<-rpart(genus~x+y+z+a+b+c, data=mydata)

Is this an R, RPART, or stataread issue?  Where did I think I read that R 
coerced character variables to factors if the context called for factor 

Dr. Marc R. Feldesman
Professor and Chairman
Anthropology Department
Portland State University
1721 SW Broadway
Portland, Oregon 97201
email:  feldesmanm at pdx.edu
phone:  503-725-3081
fax:    503-725-3905

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list