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

From: Joris Meys <jorismeys_at_gmail.com>
Date: Mon, 21 Jun 2010 14:18:55 +0200

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

______________________________________________
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.
Received on Mon 21 Jun 2010 - 12:21:54 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Jun 2010 - 13:00:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive