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
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. > > 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, > > MonicaIf all columns of your data.frame are numeric: z[z<0] <- 0 z[z>1] <- 1 Uwe Ligges
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))) > newDFx 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. > > 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 > >-- 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
Hi, Thank you so much for the little example - this was exactly what i was looking for. Just i didn't know what to search after in R. I will change it for my data needs. Thanks again, Monica
for (j in 1:n) df[,j] <- pmin( pmax( df[,j], 0 ), 1 ) should do the job. Bendix Carstensen ---------------------- Bendix Carstensen Senior Statistician Steno Diabetes Center Niels Steensens Vej 2 DK-2820 Gentofte Denmark tel: +45 44 43 87 38 mob: +45 30 75 87 38 fax: +45 44 43 07 06 bxc at steno.dk www.biostat.ku.dk/~bxc ----------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Monica > Palaseanu-Lovejoy > Sent: Wednesday, June 23, 2004 11:07 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Automatic routine - help > > > 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 >
Hi, This works beautifully as well. Thanks, Monica
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 return(z) } 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! Baz
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伮抯 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伮抰 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 : :
I note that ?min actually contains a relevant example: plot(x, pmin(cH, pmax(-cH, x)), type='b', main= "Huber's function") ^^^^^^^^^^^^^^^^^^^^^^ It's always worth timing things. The timing tests below are the best of several repetitions, to avoid worries about things like paging and GC. Make a data frame with 10 variables and 2000 cases. > m <- matrix(rnorm(10*2000), ncol=10) > colnames(m) <- LETTERS[1:10] > z <- as.data.frame(m) See how long it takes to just make a copy of z. > system.time(w <- z+0) [1] 0.08 0.01 0.16 0.00 0.00 See how long pmax(pmin(...)...) takes. > system.time(w <- pmax(pmin(z, 1), 0)) [1] 0.08 0.01 0.16 0.00 0.00 See how long the z[z < 0] <- 0; z[z > 1] <- 1 trick takes. > system.time({ w <- z; w[w < 0] <- 0; w[w > 1] <- 1 }) [1] 0.23 0.01 0.31 0.00 0.00