R changes number of decimals when unscaling

Hi. I have a very specific question about maintaining decimals when scaling and unscaling. I am making a k-means cluster component analysis with GPS coordinates. I scaled the data before conducting the analysis:

scaled_data = as.matrix(scale(GPS))

Then I made the analysis, which resulted in four clusters. Now I want to export the members of each clusters to an txt/csv file, so I can continue my analysis. I used this code, which I copied of off the internet:

unscaled.cluster1 <- t(apply(GPS, 1, 
+                              function(r) r * attr(GPS, 'scaled:scale') + 
+                                  attr(GPS, 'scaled:center')))

I then exported the members of cluster 1 to a txt file:

> sink(file = "cluster1unscaled.txt")
> unscaled.data[km$cluster==1,]
> sink(file = NULL)

The problem is that this process changed the number of decimals in the GPS coordinates, so now they are different than the original ones, which is making it impossible to continue my analysis. When I export the file to excel, it is not possible to change the number of decimals (when I try, I just get extra zeros) - this leads me to think that I need to do something in R to make sure that I get the original number of decimals back when unscaling.

Example:
Original X coordinate: 11.1373136079
New X coordinate after unscaling in R: 11.13731

I really hope someone can help, as I'm quite stuck here. Thanks! :slight_smile:

Try options(digits=10) or similar value.

Regards,
Grzegorz

Hi. Thanks for the quick reply. Where should I write this code?

Somewhere in the script, for example:

options(digits=10)
scaled_data = as.matrix(scale(GPS))
...

But to be honest, you shouldn't be worried about "truncated" number, it's just a print out of the double number. R stores the whole value in memory for computation.

I assumed that R actually used the whole value, but I just can't get it to print the whole value when I use the unscaled data

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.