开发者

How to rescale a matrix of points?

Now I'm doing it this way:

d = approx(density(csvdata[,'X'],n=5000),xout=csvdata[,'X'])
dfact = 40/max(d$y)
for(i in 1:nrow(csvdata)) {
  d$y[i] = (d$y[i]*dfact)-20
}

What I'm doing here is rescaling density function which always be highe开发者_如何学JAVAr than 0 to be displayed under from bottom of my chart which is at -20 and always fit to top that is on +20, so I'm more easisy able to spot any irregularities in the line. Now as you can see I'm doing this by looping, but maybe there are some build in one liners for it ?


y is a vector, and * and - are vectorized functions, so you don't need to loop over the vector of numbers doing the computations one at a time. Just do them all at once:

d$y <- (d$y * dfact) - 20

or better (no d$),

d <- within(d, y <- (y * dfact) - 20)

dfact is a scalar (a length 1 vector in R as it doesn't have a separate notion of scalar), but R will expand dfact (recycle is the correct terminology in R) to the required length to allow the above computations to proceed as normal.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜