I just want some random data to experiment with different prediction models.
x <- 0
for (i in 1:200)
num <- runif(1, 0, 500)
neg <- round(runif(5, -1, 0))
percent <- ((0.01 * runif(1, 1, 10)) * num)
x[i] = num + (neg * percent)
number of items to replace is not a multiple of replacement length
Chrisss has already pointed out your problem in his comment. However, you're doing a lot of bad things from an
R programming prospective. The following approach is better:
N <- 200 d <- data.frame(x = rep(NA, N)) num <- runif(N, 0, 500) neg <- sample(c(1,-1), 200, replace = TRUE) #jrdnmdhl pointed this out in his post percent <- ((0.01 * runif(N, 1, 10)) * num) d$x <- num + (neg * percent)
Why is this better? Two reasons, we are avoiding a for loop.
R is a high-level language, and therefore, loops are slow. Second, you are not preallocating your memory. Skipping this step will slow things down as well.
R has to go find more memory for each iteration in your example.