R-Randomly pick a number and do it over and over until a condition is achivied -


i want randomly pick number vector 8 elements sums 35. if number 0 number. if number greater 0, make number -1. in loop until sum of vector 20. how can in r? example: vec<-c(2,3,6,0,8,5,6,5)

pick number list randomly , make number -1 until sum of elements becomes 20.

i'm really not sure want, understand of question, here solution. you'll of concept , key fonctions in script. use , help() understand them , optimize it.

vec <- c(2, 3, 6, 0, 8, 5, 6, 5)  summ <- 0 new.vec <- null iter <- 1 while(summ<20) {  selected <- sample(vec,1)  if(selected!=0) new.vec[iter] <- selected-1  summ <- sum(new.vec)  iter <- iter+1 } 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -