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

Johannes Huesing johannes at huesing.name
Tue Jun 22 20:14:38 CEST 2010


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")



More information about the R-help mailing list