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

Liviu Andronic landronimirc at gmail.com
Thu Aug 11 20:34:43 CEST 2011


On Thu, Aug 11, 2011 at 4:58 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>>> numVars <- sapply(iris, is.numeric)
>>> iris[numVars] <- lapply(iris[numVars], round, digits = 2)
>>> head(iris)
>>>
> Then drop the assignment operation and just print:
>
> ( lapply(iris[numVars], round, digits = 2) )
>
That's a nice catch, thanks. The only problem with this approach is
that non-numeric variables are simply discarded, while I would prefer
for them to be simply ignored.


> Or set your digits argument to print:
>
> print( head(iris), digits=1)
>
> Or did I miss something?
>
As I explained in today's reply to Martin, print(..., digits = 2) and
format(..., digits = 2) both display a "minimal number of
_significant_ digits". However, for various reasons, I need to specify
the "number of decimal places" as in round(..., digits = 2). Both
print() and format() don't provide an obvious way to do that.

Regards
Liviu



More information about the R-help mailing list