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
Replacing elements of a list over a certain threshold
8 messages · James, Christos Argyropoulos, Joris Meys +4 more
"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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100621/c5012555/attachment.pl>
You shouldn't use sapply/lapply for this but use the indices
set.seed(1) r <- round(runif(100000,1,10)) treshold <- 5 head(r)
[1] 3 4 6 9 3 9
system.time( r[ r>threshold ] <- threshold )
user system elapsed
0 0 0
head(r)
[1] 3 4 5 5 3 5 On Mon, Jun 21, 2010 at 2:03 PM, Christos Argyropoulos
<argchris at hotmail.com> wrote:
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 at ipec.co.uk To: r-help at 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 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.
_________________________________________________________________ Hotmail: Free, trusted and rich email service. ? ? ? ?[[alternative HTML version deleted]] ______________________________________________ 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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100621/181aca7b/attachment.pl>
1 day later
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")
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?sing
David Winsemius, MD West Hartford, CT
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of David Winsemius Sent: Tuesday, June 22, 2010 11:24 AM To: johannes at huesing.name Cc: r-help at r-project.org Subject: Re: [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)
Is it essential that the dataset 'example' be a 'list'
and not a 'numeric' object (created in this case by
calling 'c' instead of 'list')?
list's can contain elements of various types and there
are usually time and memory penalties for allowing that
flexibility. numeric (or character or complex or logical)
objects contain only one type of element and generally
use less space than the equivalent list and processing them
generally takes less time.
E.g., here are timings for several of the algorithms
that have been suggested on equivalent list and numeric
objects of length 10^5:
> x.orig <- runif(10^5, min=0, max=10) # numeric object
> xl.orig <- as.list(x.orig) # list object, one scalar numeric vector per element
> x <- x.orig ; system.time(x[x>5] <- 5)
user system elapsed
0.000 0.000 0.004
> x <- x.orig ; system.time(x <- pmin(x, 5))
user system elapsed
0.010 0.000 0.002
> xl <- xl.orig ; system.time(xl[xl>5] <- 5)
user system elapsed
0.020 0.000 0.013
> xl <- xl.orig ; system.time(xl <- pmin(xl, 5))
user system elapsed
0.080 0.000 0.084
> xl <- xl.orig ; system.time(xl <- lapply(xl, min, 5))
user system elapsed
0.130 0.000 0.135
In addition to the time penalty, it just seems unnatural
to use a list to store numbers when a numeric object could
do the job.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
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?sing
David Winsemius, MD West Hartford, CT
______________________________________________ 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.