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? ----- ------------------------------------------------------------------ Saji Ren from Shanghai China GoldenHeart Investment Group ------------------------------------------------------------------ -- View this message in context: http://r.789695.n4.nabble.com/How-to-perform-a-substitution-in-a-loop-tp2325048p2325048.html Sent from the R help mailing list archive at Nabble.com.
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
You need {} for (i in 1:500){ .......commands.......} Like: x<-rep(c(0.8,1.2),250) for (i in 1:500) { if (x[i]>1) x[i]=1 } BR JEsper -- View this message in context: http://r.789695.n4.nabble.com/How-to-perform-a-substitution-in-a-loop-tp2325048p2325073.html Sent from the R help mailing list archive at Nabble.com.
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? Saji ----- ------------------------------------------------------------------ Saji Ren from Shanghai China GoldenHeart Investment Group ------------------------------------------------------------------ -- View this message in context: http://r.789695.n4.nabble.com/How-to-perform-a-substitution-in-a-loop-tp2325048p2325193.html Sent from the R help mailing list archive at Nabble.com.
Reasonably Related Threads
- Problem about Box-Cox transformation (topic in html form)
- Confusion in 'quantile' and getting rolling estimation of sample quantiles
- Help with function "fitdistr" in "MASS"
- Problem about SARMA model forcasting
- How to compute Rolling analysis of Standard Deviation using ZOO package?