[R] How to perform a substitution in a loop?

Peter Dalgaard pdalgd at gmail.com
Sat Aug 14 09:57:26 CEST 2010


Saji Ren wrote:
> Hello all:
> 
> I have a data series of 500 data, and I want to limit the value of it to be
> less than 1.
> Below is my code:
> 
>> for (i in 1:500)
> +if( x[i] > 1)
> +x[i] = 1
> 
> but the system told me it's wrong. Can anyone told me the reason?

Works for me, so there's something you're not telling us.... Possibly
that x is not a vector (a data frame, maybe?).

(And "it's wrong" is not one of the official error messages in any
language. Posting guide, reproducible code, etc.)

The whole thing is an inefficient way to do  x <- pmin(x,1), though.

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list