[R] How to perform a substitution in a loop?
Peter Dalgaard
pdalgd at gmail.com
Sat Aug 14 16:01:29 CEST 2010
Saji Ren wrote:
> Hello, Mr Dalgaard:
>
> you're right about the problem.
>
> "Works for me, so there's something you're not telling us.... Possibly
> that x is not a vector (a data frame, maybe?)."
>
> the mistake message is below:
> Error in if (track01[i] > 1) track01[i] <- 1 :
> missing value where TRUE/FALSE needed
>
> the track01 above is my data, I've used a rnorm(500) data series instead,
> and the code works!!
>
> One thing I do know is that my "track01" data has NaN data.
> So I guess the system can not perform the code " if (track01[i] > 1)" when
> track01[i]=NaN, or the result is missing value, thus it need a TRUE/FALSE
>
> is there any command to replace the "NaN" value with 1 in an efficient way?
Usually, you'd safeguard the if with "if (is.na(x[i]) || x[i] > 1)..."
or "if (!is.na(...) && ...)", depending on what you want.
BTW, notice also that pmin has an na.rm argument.
--
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