Re: [R] Replacing elements of a list over a certain threshold

2010-06-22 Thread William Dunlap
> -Original Message-
> From: r-help-boun...@r-project.org 
> [mailto:r-help-boun...@r-project.org] On Behalf Of David Winsemius
> Sent: Tuesday, June 22, 2010 11:24 AM
> To: johan...@huesing.name
> Cc: r-help@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  [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
> >> 
> >> 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@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.
> 

__
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.


Re: [R] Replacing elements of a list over a certain threshold

2010-06-22 Thread David Winsemius


On Jun 22, 2010, at 2:14 PM, Johannes Huesing wrote:

Jim Hargreaves  [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

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@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.


Re: [R] Replacing elements of a list over a certain threshold

2010-06-22 Thread Johannes Huesing
Jim Hargreaves  [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
> 
> 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:johan...@huesing.name  from such a trifling investment of fact.  
  
http://derwisch.wikidot.com (Mark Twain, "Life on the Mississippi")

__
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.


Re: [R] Replacing elements of a list over a certain threshold

2010-06-21 Thread Henrique Dallazuanna
Try this also:

replace(example, example > threshold, threshold)

On Mon, Jun 21, 2010 at 7:34 AM, Jim Hargreaves  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
> 
> 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]]

__
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.


Re: [R] Replacing elements of a list over a certain threshold

2010-06-21 Thread Joris Meys
You shouldn't use sapply/lapply for this but use the indices

> set.seed(1)
> r <- round(runif(10,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
 wrote:
>
> Hi,
> You should use the sapply/lapply for such operations.
>
>> r<-round(runif(10,1,10))
>> head(r)
> [1] 3 7 6 3 2 8
>> filt<-function(x,thres) ifelse(x> 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: ja...@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
>> 
>> 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]]
>
> __
> 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.
>



-- 
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.m...@ugent.be
---
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

__
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.


Re: [R] Replacing elements of a list over a certain threshold

2010-06-21 Thread Christos Argyropoulos

Hi, 
You should use the sapply/lapply for such operations.

> r<-round(runif(10,1,10))
> head(r)
[1] 3 7 6 3 2 8
> filt<-function(x,thres) ifelse(x system.time(r2<-sapply(r,filt,thres=5))
   user  system elapsed 
   3.360.003.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: ja...@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
> 
> 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]]

__
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.


Re: [R] Replacing elements of a list over a certain threshold

2010-06-21 Thread Joris Meys
"magic" code:
example[example>threshold] <-threshold

Cheers
Joris

On Mon, Jun 21, 2010 at 12:34 PM, Jim Hargreaves  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
> 
> 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.
>



-- 
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.m...@ugent.be
---
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

__
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.