samedi 9 avril 2016

Generating sample data in a function in r without repeat

I have the following reproducible code:

x <- as.data.table(c(1:20))
samplefunction <- function(x) { 
if(x <= 10 ) { 
sample(35:45)
}else if(x > 10) { 
sample(50:60)
}
}

day1 <- x[,sales := sapply(x, samplefunction)]
day2 <- x[,sales := sapply(x, samplefunction)]

Both day1 and day2 populate the sample value for sales. It looks like the value returned from samplefunction is re-used for each iteration. How can I create seperate data frames (day1, day2,...) and re-generate random data for the sales column each time? Set.Seed() is used to re-use the same random generation..I'm looking for the opposite, in a sense. Thank you.




Aucun commentaire:

Enregistrer un commentaire