[R] read.table() and scientific notation

Alex Brown
Tue, 10 Oct 2006 12:00:04 -0000

A cheeky solution by subverting the coerce mechanism and read.table:

# install a coerce function which can fix the "e+10" syntax for an
imaginary class myDouble:

 > setAs("character", "myDouble",
function(from)as.double(sub('^(-?)
e','\\11e',from)))
Warning message:
in the method signature for function 'coerce' no definition for
class: ?myDouble? in: matchSignature(signature, fdef, where)

# load some data:

 > Lines <- scan(sep="\n", what="")
a 1 3e-8
b 2 1e+10
c 3 e-10
d 4 e+3
e 5 e+1

# process it without using the imaginary class - use a real double
instead to see what happens:
# Note I've used textConnection(Lines) here, where your filename
would go

 > T <- read.table(textConnection(Lines), colClasses=list
("character", "integer", "double"))
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines,
na.strings,  :
	scan() expected 'a real', got 'e-10'

# process it, specifying the imaginary class myDouble.

 > T <- read.table(textConnection(Lines), colClasses=list
("character", "integer", "myDouble"))
 > T
   V1 V2    V3
1  a  1 3e-08
2  b  2 1e+10
3  c  3 1e-10
4  d  4 1e+03
5  e  5 1e+01

 > lapply(T, class)
$V1
[1] "character"

$V2
[1] "integer"

$V3
[1] "numeric"


Someone's bound to shoot me down for hackery here :-)

-Alex

On 10 Oct 2006, at 11:43, January Weiner wrote:

> Dear all,
>
> I am having troubles importing values written as scientific notation
> using read.table(). I'm sure this is a frequent problem, as many
> people in my lab have this problem as well, so I'm sure that I just
> have troubles googling for the right solution.
>
> The problem is, that, given a file like that:
>
> a 1 2e-4
> b 2 3e-8
> ...
>
> the third column gets imported as a factor, or a string if I set the
> as.is parameter of read.table to TRUE for this column. However, I just
> want a simple numeric vector :-) I'm sure there is a simple trick for
> this. If you can point me to the right function, or manual, I think I
> should be able to find out the details myself.
>
> Thanks in advance,
> January
>
> --
> ------------ January Weiner 3  ---------------------+---------------
> Division of Bioinformatics, University of Muenster  |  Schlo?platz 4
> (+49)(251)8321634                                   |  D48149 M?nster
> http://www.uni-muenster.de/Biologie.Botanik/ebb/    |  Germany
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.