Skip to content
Prev 247375 / 398503 Next

Rounding variables in a data frame

Hi Pete,

Here is one option.  The first part of sapply() just serves to get all
names of d except those you excluded in 'exc'.  If you were including
fewer variables than you were excluding, just get rid of the logical !
operator.

d <- data.frame(d1 = rnorm(10, 10), d2 = rnorm(10, 6),
  d3 = rnorm(10, 2), d4 = rnorm(10, -4))

exc <- "d3"

cbind(d[, exc, drop = FALSE],
  sapply(names(d)[!names(d) %in% exc], function(x) round(d[, x])))


HTH,

Josh
On Fri, Jan 14, 2011 at 8:53 PM, Pete B <Peter.Brecknock at bp.com> wrote: