[R] Rounding variables in a data frame

Joshua Wiley jwiley.psych at gmail.com
Sat Jan 15 15:11:27 CET 2011


On Fri, Jan 14, 2011 at 11:16 PM, Phil Spector
<spector at stat.berkeley.edu> wrote:
> Is sapply really necessary here?

Apparently not, and it is certainly more cumbersome.  Because data
frames can contain a mix of classes, I thought that round() did not
have a method for them (in retrospect that does not make much sense).
I had also not seen Dr. Venables reply.

>
>> exc = !names(d) %in% "d3"

Thanks for sharing---this circumvents the other problem I had of
needing to pass the actual character names.

>> d[,exc] = round(d[,exc])
>> d
>
>   d1 d2         d3 d4
> 1  10  6  2.3749642 -4
> 2  11  6 -0.2081097 -2
> 3  10  4  1.2675955 -4
> 4  10  8  1.2468859 -2
> 5  10  6  2.7193027 -4
> 6   9  6  1.9195531 -5
> 7   9  6  2.8188036 -6
> 8  10  7  2.5755148 -4
> 9  11  5  2.4037376 -6
> 10 11  5  3.4295063 -5
>
>                                        - Phil Spector
>                                         Statistical Computing Facility
>                                         Department of Statistics
>                                         UC Berkeley
>                                         spector at stat.berkeley.edu

Thanks,

Josh


More information about the R-help mailing list