[R] Automatic routine - help
Prof Brian Ripley
ripley at stats.ox.ac.uk
Wed Jun 23 11:15:54 CEST 2004
How about
newDF <- DF
newDF[] <- lapply(DF, function(x) pmax(0, pmin(x, 1)))
as in
> DF <- data.frame(x=rnorm(5), y=rnorm(5))
> newDF <- DF
> newDF[] <- lapply(DF, function(x) pmax(0, pmin(x, 1)))
> newDF
x y
1 0.31872426 1.0000000
2 1.00000000 0.0000000
3 1.00000000 0.4510969
4 0.04753697 1.0000000
5 0.89016978 0.0000000
On Wed, 23 Jun 2004, 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.
>
> Lets 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 dont 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
>
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list