[R] round() a data frame containing 'character' variables?

Liviu Andronic landronimirc at gmail.com
Thu Aug 11 19:29:43 CEST 2011


On Wed, Aug 10, 2011 at 5:52 PM, Jean V Adams <jvadams at usgs.gov> wrote:
>
> 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.
>
I did inspect the help page (several times), but the "significant
digits" formulation was a little too cryptic to me. Now I understand
better, thanks.


> 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))
>
Quite awkward, but this is more or less what I was looking for. Again, thanks.

Regards
Liviu



More information about the R-help mailing list