Skip to content

R 1.2.1 - read.table - factors problem or is it a data.frame problem

2 messages · gordon.harrington@uni.edu, Thomas Lumley

#
Patrick Connolly refers to the read.table help manual page to show how to
coerce input columns to character or to numeric. Indeed coercion with a logical
vector will set the mode regardless of the column content. He also notes one
can set factors with factor(). 

However, the problem encountered is not one of setting factors but of unsetting
them. The manual states that variables of mode or type character will become
factors. My data input efforts showed no relationship between type and factor.
With no evident reason, most character variables did not become factors while
many real variables did. It is a bit disconcerting to get an output with
thousands of floating point factor levels or error messages that one's data are
of the wrong mode for any analysis whatsoever.

How does one unset mode assignment of factor and how does one avoid the problem
of automatic misassignment with other datasets?

Gordon
Gordon M. Harrington		Mail:	3720 Village Place, #6308
Professor Emeritus			Waterloo, IA 50702-5848
University of Northern Iowa 	Phone:	319-291-8535
gordon.harrington at uni.edu	Fax:	319-291-8491
dryfly at aya.yale.edu			319-291-8324

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Wed, 31 Jan 2001 gordon.harrington at uni.edu wrote:
You can convert a factor to the correct numeric values with
   as.numeric(as.character(the.factor))

We don't have enough information to tell what happened in your case but in
my experience the most common reason for a numeric variable to read as a
factor has been misspecifying the missing value codes in the na.strings
argument. This argument lists the strings that should be converted to NAs;
any other strings will trigger a conversion to factor.

	-thomas

Thomas Lumley			Asst. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._