Reputation: 29377
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 ?
Upvotes: 3
Views: 300
Reputation: 174778
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.
Upvotes: 3