You seem to be in Circle 1 of 'The R Inferno'.
Your technique does work, just not the
way that you expect. Try doing:
range( (mat * 100) %% 1)
The 'zapsmall' function might be of interest
as well.
On 26/03/2010 21:05, JustinNabble wrote:
Can anyone explain this?
I have a matrix with double components. It's taking up a lot of memory, so I
want to multiply then turn it to integers. I'm pretty certain that there are
only 2 decimal places, but I wanted to check by using modulo. E.g.
mat = matrix(11:50/100, ncol=4,nrow=10) #Matrix with values out to the
hundredths
any((mat * 100)%%1!=0)
But oddly enough it doesn't work. Even in this simple example the result I
get is this:
[,1] [,2] [,3] [,4]
[1,] 0.000000e+00 0.000000e+00 0 0
[2,] 0.000000e+00 0.000000e+00 0 0
[3,] 0.000000e+00 0.000000e+00 0 0
[4,] 1.776357e-15 0.000000e+00 0 0
[5,] 0.000000e+00 0.000000e+00 0 0
[6,] 0.000000e+00 0.000000e+00 0 0
[7,] 0.000000e+00 0.000000e+00 0 0
[8,] 0.000000e+00 3.552714e-15 0 0
[9,] 0.000000e+00 1.000000e+00 0 0
[10,] 0.000000e+00 0.000000e+00 0 0
Two non-zero values are just very small, but one is value is actually 1. Can
someone explain this?
If you pick just a single number you can see some odd results too.
(4.1*100)%/%1
[1] 409
(4.1*10*10)%/%1
[1] 410
Shouldn't the result be 410 each time?
I think in this case it should have returned all 0s, and I could have done
something like
newmat = as.integer(mat*100)
dim(newmat) = dim(mat)
rm(mat)
Is there a better way to convert my double matrix to an integer matrix
without losing precision? Or are there better ways to conserve memory? I'm
at the limit.
Thanks,
Justin
--
Patrick Burns
pbu...@pburns.seanet.com
http://www.burns-stat.com
(home of 'Some hints for the R beginner'
and 'The R Inferno')
______________________________________________
R-help@r-project.org 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.