How to rescale the matrix of points?

Now I do this:

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 scaling the density function, which will always be above 0 to display at the bottom of the bottom of my chart, which is at -20 and always goes to the top that is at +20, so I'm more simplified able detect any line irregularities. Now, as you can see, I am doing this, looping, but maybe there are several buildings in it?

+3
source share
1 answer

yis a vector, and *and -are vectorized functions, so you do not need to loop a vector of numbers that perform calculations one at a time. Just do it all at once:

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

or better (no d$)

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

dfact ( 1 R, ), R dfact ( R) .

+3

Source: https://habr.com/ru/post/1779566/


All Articles