Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rescale a matrix of points?

Tags:

r

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 higher 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 ?

like image 252
rsk82 Avatar asked Dec 05 '25 15:12

rsk82


1 Answers

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.

like image 60
Gavin Simpson Avatar answered Dec 08 '25 03:12

Gavin Simpson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!