[R] Automatic routine - help

Gabor Grothendieck ggrothendieck at myway.com
Wed Jun 23 18:10:14 CEST 2004


You already have a few solutions but here is one more:

   (z >= 0 & z <= 1) * z + (z > 1)

It uses the fact that TRUE and FALSE act as 1 and 0 in arithmetic.

Slightly shorter but trickier is:

   (z*z <= z) * z + (z > 1)

which uses the fact that z*z <= z only in the interval [0,1] .

Monica Palaseanu-Lovejoy <monica.palaseanu-lovejoy <at> stud.man.ac.uk> writes:

: 
: 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.
: 
: LetÂ’s suppose I have a data.frame with j columns (from 1 to n) and i 
: rows (from 1 to p). I would like to write a procedure which reads 
: every column j (j from 1 to n) and compare each value with the 
: interval [0,1]. If z(i,j) is less than 0, then replace z(i,j) with 0. If z
(i,j) 
: is greater than 1, then replace z(i,j) with 1. If z(i,j) is inside the 
: interval [0,1] then donÂ’t change. In the end I would like to have a 
: new data.frame with the new values. 
: 
: I am not sure how complicated or long such a procedure might be, 
: so I will be very grateful for any help.
: 
: Thank you in advance,
: 
: Monica
: 
: ______________________________________________
: R-help <at> stat.math.ethz.ch mailing list
: https://www.stat.math.ethz.ch/mailman/listinfo/r-help
: PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
: 
:




More information about the R-help mailing list