[Rd] beware of sapply(x, data.class) == "numeric"

Martin Maechler Martin Maechler <maechler@stat.math.ethz.ch>
Tue, 6 Nov 2001 10:59:19 +0100


This is just a warning/hint to others; package/function writers in
particular : 

The `cluster' package (originally S-plus from Rousseeuw et al) currently 
has code such as

	## check type of input matrix
	if((!is.data.frame(x) && !is.numeric(x)) ||
	   !all(sapply(x, data.class) == "numeric"))
	    stop("x is not a numeric dataframe or matrix.")
	x <- data.matrix(x)

This was naver quite correct (see below), but with the 
new (R-devel or R-1.4.0) behavior of  read.table() becomes "fatal".
The new read.table() behavior does easily return data.frame()s with integer
component vectors {which is nice; among others, half memory!}.
such that the above test fails.

I think I have seen the ``idiom'' 

  all(sapply(x, data.class) == "numeric")

in other places too, so beware!  In most cases, this probably should be

  all(sapply(x, data.class) %in% c("numeric","integer","logical"))

A better alternative in most cases would be something like

  { x <- data.matrix(x);  is.numeric(x) || is.logical(x) }


Martin Maechler <maechler@stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO D10	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel 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-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._