# [R] digits in matrix

Duncan Murdoch murdoch.duncan at gmail.com
Fri Jan 30 14:51:08 CET 2015

```On 30/01/2015 8:17 AM, Knut Krueger wrote:
> Am 30.01.2015 um 12:51 schrieb Duncan Murdoch:
>
> > You are mixing up formatting with storage.  Floating point numbers will
> > be displayed without decimals if they are close enough to whole numbers.
> >
> > Duncan Murdoch
> >
>
> Ok, I am talking from display
>
> data = matrix(c(1:16),nrow=4,ncol=4) #create matrix
> data[4,] = data[4,]/3
> data[,4] = data[,4]/3
> #why is it displayed without decimals here:
> data[1:3,1:3]
> #and why is data[1:3,1:3] diplayed with 2 decimals here
> data
>
> and what could be the solution to display data[1:3,1:3] part when data
> is used  without decimals, if there are no

The default formatting for matrices uses the same number of decimal
places for all entries in a column.  In the first case, none needed any
decimals to get options("digits") worth of precision, so none were
displayed.  In the second case, entries in row 4 need 6 decimal places
(on my system, maybe differently on yours) in columns 1, 2, and 4, so
all values in those columns are displayed with 6 decimal places.

If you want formatting to work differently, you can write your own. For
example, if you define

myprint <- function(x, ...) {
result <- sub("[.][0]+", "", format.default(x, ...))
print(noquote(result))
}

then

myprint(data)

might do what you want.

Duncan Murdoch

```