As it says in

?format

the digits argument specifies "... how many significant digits are to be 
used ... enough decimal places will be used so that the smallest (in 
magnitude) number has this many significant digits ..."

In your example, the last value in column "a" is 0.06348058 which is 
written as 0.063 to two significant digits.

There is no way (that I know of) to make the format() function do the same 
sort of thing as round().

If digits won't meet your needs, you could try something like this

data.frame(lapply(x, function(y) if(is.numeric(y)) round(y, 2) else y))

Jean

`·.,,  ><(((º>   `·.,,  ><(((º>   `·.,,  ><(((º>

Jean V. Adams
Statistician
U.S. Geological Survey
Great Lakes Science Center
223 East Steinfest Road
Antigo, WI 54409  USA

Liviu Andronic <landronim...@gmail.com> wrote on 08/10/2011 10:26:43 AM:

> [image removed] 
> 
> Re: [R] round() a data frame containing 'character' variables?
> 
> Liviu Andronic 
> 
> to:
> 
> Jean V Adams
> 
> 08/10/2011 10:27 AM
> 
> Cc:
> 
> "r-help@r-project.org Help"
> 
> Hello
> 
> On Wed, Aug 10, 2011 at 2:31 PM, Jean V Adams <jvad...@usgs.gov> wrote:
> > The function format() might serve your needs.
> >
> This looks very promising, but yields some strange results. See below:
> 
> > x <- data.frame(a=rnorm(10), b=rnorm(10), c=rnorm(10), 
d=letters[1:10])
> > x
>              a           b            c d
> 1   0.54114449 -0.11195580  1.526279364 a
> 2   3.27109063  0.50848249 -0.215760332 b
> 3  -0.27064475 -1.04749725  0.082319811 c
> 4  -0.06638611 -0.58600572  0.004148253 d
> 5  -0.06170739 -0.37885203  0.689125494 e
> 6   0.53211363 -0.09150913 -0.463972307 f
> 7  -0.43314431 -0.28981614 -0.973410994 g
> 8   0.52137857 -1.15077343  0.163120205 h
> 9  -1.39581552  1.27378389  0.136708313 i
> 10  0.06348058 -0.00369746 -0.570214119 j
> > format(x, digits=2)  ##it displays 3 or 4 digits, instead of the 
required 2
>         a       b       c d
> 1   0.541 -0.1120  1.5263 a
> 2   3.271  0.5085 -0.2158 b
> 3  -0.271 -1.0475  0.0823 c
> 4  -0.066 -0.5860  0.0041 d
> 5  -0.062 -0.3789  0.6891 e
> 6   0.532 -0.0915 -0.4640 f
> 7  -0.433 -0.2898 -0.9734 g
> 8   0.521 -1.1508  0.1631 h
> 9  -1.396  1.2738  0.1367 i
> 10  0.063 -0.0037 -0.5702 j
> > format(x, digits=2, nsmall=1, scientific=7)  ##no change when 
> setting related arguments
>         a       b       c d
> 1   0.541 -0.1120  1.5263 a
> 2   3.271  0.5085 -0.2158 b
> 3  -0.271 -1.0475  0.0823 c
> 4  -0.066 -0.5860  0.0041 d
> 5  -0.062 -0.3789  0.6891 e
> 6   0.532 -0.0915 -0.4640 f
> 7  -0.433 -0.2898 -0.9734 g
> 8   0.521 -1.1508  0.1631 h
> 9  -1.396  1.2738  0.1367 i
> 10  0.063 -0.0037 -0.5702 j
> > round(x[1:3], digits=2)  ##works as expected
>        a     b     c
> 1   0.54 -0.11  1.53
> 2   3.27  0.51 -0.22
> 3  -0.27 -1.05  0.08
> 4  -0.07 -0.59  0.00
> 5  -0.06 -0.38  0.69
> 6   0.53 -0.09 -0.46
> 7  -0.43 -0.29 -0.97
> 8   0.52 -1.15  0.16
> 9  -1.40  1.27  0.14
> 10  0.06  0.00 -0.57
> 
> Any ideas why format() and round() give so different results? Can
> format() be set to behave similarly to round? Regards
> Liviu

        [[alternative HTML version deleted]]

______________________________________________
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.

Reply via email to