[R] read.table / type.convert: Unexpected column class

Andreas Borg andreas.borg at unimedizin-mainz.de
Tue Nov 23 14:12:03 CET 2010


Hi all,

I stumbled across the following, in my opinion surprising, behaviour of 
read.table:

 > data <- read.table(stdin(), sep=",")
0: 1, 2, 3 # Some comment
1:
 > sapply(data, class)
       V1        V2        V3
"integer" "integer" "numeric"

I was surprised to find that V3 has class "numeric" despite its apparent 
integer value. I took a look at type.convert (which is called by 
read.data to determine column classes) for explanation (I quote from the 
man page):

    "Vectors containing optional whitespace followed by decimal 
constants representable as R integers [...] are converted to integer. 
Other vectors containing optional whitespace followed by other decimal 
or hexadecimal constants [...] are converted to numeric."

Obviously the trailing whitespace between '3' and the comment sign '#' 
is treated as "other decimal or hexadecimal constant" while the leading 
whitespace before "2" is stripped. Actually, leaving just the trailing 
space after '3' without the comment gives the same result. Is there any 
reason for this behaviour?

Best regards,

Andreas

-- 
Andreas Borg
Medizinische Informatik

UNIVERSITÄTSMEDIZIN
der Johannes Gutenberg-Universität
Institut für Medizinische Biometrie, Epidemiologie und Informatik
Obere Zahlbacher Straße 69, 55131 Mainz
www.imbei.uni-mainz.de

Telefon +49 (0) 6131 175062
E-Mail: borg at imbei.uni-mainz.de

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der
richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den
Absender und löschen Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe
dieser Mail und der darin enthaltenen Informationen ist nicht gestattet.



More information about the R-help mailing list