Jim Hargreaves
2010-Jun-21 10:34 UTC
[R] Replacing elements of a list over a certain threshold
Dear List, I have a list of length ~1000 filled with numerics. I need to replace the elements of this list that are above a certain numerical threshold with the value of the threshold. e.g example=list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) threshold=5 <magic code goes here> example=(1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 3, 2, 1). I have written a crude script that achieves this but it's very slow. Is there a way to do this using some R function? Crude script: http://pastebin.com/3KSfi8nD
Joris Meys
2010-Jun-21 10:47 UTC
[R] Replacing elements of a list over a certain threshold
"magic" code: example[example>threshold] <-threshold Cheers Joris On Mon, Jun 21, 2010 at 12:34 PM, Jim Hargreaves <james at ipec.co.uk> wrote:> Dear List, > > I have a list of length ~1000 filled with numerics. I need to replace the > elements of this list that are above a certain numerical threshold with the > value of the threshold. > > e.g > example=list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) > threshold=5 > <magic code goes here> > example=(1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 3, 2, 1). > > I have written a crude script that achieves this but it's very slow. Is > there a way to do this using some R function? > > Crude script: http://pastebin.com/3KSfi8nD > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Joris Meys Statistical consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control tel : +32 9 264 59 87 Joris.Meys at Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
Christos Argyropoulos
2010-Jun-21 12:03 UTC
[R] Replacing elements of a list over a certain threshold
Hi, You should use the sapply/lapply for such operations.> r<-round(runif(100000,1,10)) > head(r)[1] 3 7 6 3 2 8> filt<-function(x,thres) ifelse(x<thres,x,thres) > system.time(r2<-sapply(r,filt,thres=5))user system elapsed 3.36 0.00 3.66> head(r2)[1] 3 5 5 3 2 5 To return a list, replace "sapply" with "lapply" Christos> Date: Mon, 21 Jun 2010 11:34:01 +0100 > From: james@ipec.co.uk > To: r-help@r-project.org > Subject: [R] Replacing elements of a list over a certain threshold > > Dear List, > > I have a list of length ~1000 filled with numerics. I need to replace > the elements of this list that are above a certain numerical threshold > with the value of the threshold. > > e.g > example=list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) > threshold=5 > <magic code goes here> > example=(1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 3, 2, 1). > > I have written a crude script that achieves this but it's very slow. Is > there a way to do this using some R function? > > Crude script: http://pastebin.com/3KSfi8nD > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code._________________________________________________________________ Hotmail: Free, trusted and rich email service. [[alternative HTML version deleted]]
Henrique Dallazuanna
2010-Jun-21 13:19 UTC
[R] Replacing elements of a list over a certain threshold
Try this also: replace(example, example > threshold, threshold) On Mon, Jun 21, 2010 at 7:34 AM, Jim Hargreaves <james@ipec.co.uk> wrote:> Dear List, > > I have a list of length ~1000 filled with numerics. I need to replace the > elements of this list that are above a certain numerical threshold with the > value of the threshold. > > e.g > example=list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) > threshold=5 > <magic code goes here> > example=(1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 3, 2, 1). > > I have written a crude script that achieves this but it's very slow. Is > there a way to do this using some R function? > > Crude script: http://pastebin.com/3KSfi8nD > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Johannes Huesing
2010-Jun-22 18:14 UTC
[R] Replacing elements of a list over a certain threshold
Jim Hargreaves <james at ipec.co.uk> [Mon, Jun 21, 2010 at 12:34:01PM CEST]:> Dear List, > > I have a list of length ~1000 filled with numerics. I need to > replace the elements of this list that are above a certain numerical > threshold with the value of the threshold. > > e.g > example=list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) > threshold=5 > <magic code goes here> > example=(1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 3, 2, 1). >lapply(example, min, 5) -- Johannes H?sing There is something fascinating about science. One gets such wholesale returns of conjecture mailto:johannes at huesing.name from such a trifling investment of fact. http://derwisch.wikidot.com (Mark Twain, "Life on the Mississippi")
David Winsemius
2010-Jun-22 18:24 UTC
[R] Replacing elements of a list over a certain threshold
On Jun 22, 2010, at 2:14 PM, Johannes Huesing wrote:> Jim Hargreaves <james at ipec.co.uk> [Mon, Jun 21, 2010 at 12:34:01PM > CEST]: >> Dear List, >> >> I have a list of length ~1000 filled with numerics. I need to >> replace the elements of this list that are above a certain numerical >> threshold with the value of the threshold. >> >> e.g >> example=list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, >> 1) >> threshold=5 >> <magic code goes here> >> example=(1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 3, 2, 1). >> > > lapply(example, min, 5)Perhaps wrapped in unlist( ) if a vector is desired. The same strategy would work with pmin and probably be faster (albeit not a big deal if the list is only 1000 elements long: unlist( pmin(example, 5) )> > -- > Johannes H?singDavid Winsemius, MD West Hartford, CT