Adding Noise to a Numeric Vector in R Programming – jitter() Function
Last Updated :
19 Jun, 2020
In R programming, jittering means adding small amount of random noise to a numeric vector object. In this article, we’ll learn to use jitter()
function and create a plot to visualize them.
Syntax: jitter(x, factor)
Parameters:
x: represents numeric vector
factor: represents numeric value for factor specification
Example 1:
x <- round ( runif (1000, 1, 10))
y <- x + rnorm (1000, mean = 0, sd = 5)
png (file= "withoutJitter.png" )
plot (x, y, xlim = c (0, 11),
main = "Without Jitter Function" )
dev.off ()
x_j <- jitter (x)
png (file= "withJitter.png" )
plot (x_j, y, xlim = c (0, 11),
main = "With Jitter Function" )
dev.off ()
|
Output:
Example 2: With large factor value
x <- round ( runif (1000, 1, 10))
y <- x + rnorm (1000, mean = 0, sd = 5)
png (file= "withoutJitterFactor.png" )
plot (x, y, xlim = c (0, 11),
main = "Without Jitter Function" )
dev.off ()
x_j <- jitter (x, factor = 2)
png (file= "withJitterFactor.png" )
plot (x_j, y, xlim = c (0, 11),
main = "With Jitter Function and Large Factor" )
dev.off ()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...