[R] conditional replacement

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Tue May 23 20:52:02 CEST 2006



Sachin J wrote:
> Hi 
>    
>   How can do this in R.
>    
>   >df 
>    
>   48        
>   1          
>   35        
>   32        
>   80
>    
>   If df < 30  then replace it with 30 and else if df > 60 replace it with 60. I have a large dataset so I cant afford to identify indexes and then replace. 
>   Desired o/p:
>    
>   48
>   30
>   35
>   32
>   60
>    
>   Thanx in advance.
>    
> Sachin
> __________________________________________________
> 
> 

Try:

pmax(pmin(df, 60), 30)

assuming "df" is numeric (and not a data.frame). ifelse is also an option.

--sundar



More information about the R-help mailing list