[R] Automatic routine - help

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Wed Jun 23 12:24:12 CEST 2004

Uwe Ligges wrote:
> Monica Palaseanu-Lovejoy wrote:
>> Hi,
>> I would like to write a little automatic routine in R, but i am a too 
>> much of a beginner for that. I will appreciate any help regarding this 
>> particular problem.

> If all columns of your data.frame are numeric:
> z[z<0] <- 0
> z[z>1] <- 1

  For added fun, you can wrap any of the methods given on the list into 
a function. For example:

  hardLimit <- function(z, min=0, max=1){
    z[z < min] <- min
    z[z > max] <- max

  Then you can do:

  z <- hardLimit(z)

  if you want to overwrite z, or:

  y <- hardLimit(z)

  to create a new data frame.

  Note how the default min and max arguments are 0 and 1, and make the 
function more flexible. You can also do:

  x <- hardLimit(z, min=-1)

  and that sets everything below -1 to the value -1.

Welcome to the world of R development!


More information about the R-help mailing list