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

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Tue, 22 Jun 2010 14:24:07 -0400

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

> Jim Hargreaves <james@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



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 Tue 22 Jun 2010 - 18:28:30 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 Tue 22 Jun 2010 - 19: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