# Suppose I have a vector: myvec = c(1,0,3,0,77,9,0,1,2,0) # I want to randomly pick an element from myvec # where element == 0 # and print the value of the corresponding index. # So, for example I might randomly pick the 3rd 0 # and I would print the corresponding index # which is 7, # My initial approach is to use a for-loop. # Also I take a short-cut which assumes myvec is short: elm = 1 while (elm != 0) { # Pick a random index, (it might be a 0): rndidx = round(runif(1, min=1, max=length(myvec))) elm = myvec[rndidx] if(elm == 0) print("I am done") else print("I am not done") } print(rndidx) # If myvec is large and/or contains no zeros, # The above loop is sub-optimal/faulty. # I suspect that skilled R-people would approach this task differently. # Perhaps they would use features baked into R rather than use a loop? [[alternative HTML version deleted]]
Not only does it not require a loop, this is a one-liner:> myvec <- c(1,0,3,0,77,9,0,1,2,0) > sample(which(myvec == 0), 1)[1] 4> sample(which(myvec == 0), 1)[1] 7> sample(which(myvec == 0), 1)[1] 2 If there's a possibility of not having zeros then you'll need to check that separately, otherwise sample() will throw an error. For instance: if(any(myvec == 0)) { sample(which(myvec == 0), 1) } which() will Sarah On Thu, Oct 17, 2013 at 2:54 PM, Stock Beaver <stockbeaver at ymail.com> wrote:> # Suppose I have a vector: > > myvec = c(1,0,3,0,77,9,0,1,2,0) > > # I want to randomly pick an element from myvec > # where element == 0 > # and print the value of the corresponding index. > > # So, for example I might randomly pick the 3rd 0 > # and I would print the corresponding index > # which is 7, > > # My initial approach is to use a for-loop. > # Also I take a short-cut which assumes myvec is short: > > elm = 1 > while (elm != 0) { > # Pick a random index, (it might be a 0): > rndidx = round(runif(1, min=1, max=length(myvec))) > elm = myvec[rndidx] > if(elm == 0) > print("I am done") > else > print("I am not done") > } > print(rndidx) > > # If myvec is large and/or contains no zeros, > # The above loop is sub-optimal/faulty. > > # I suspect that skilled R-people would approach this task differently. > # Perhaps they would use features baked into R rather than use a loop? > [[alternative HTML version deleted]] >-- Sarah Goslee http://www.functionaldiversity.org
On 10/17/2013 11:54 AM, Stock Beaver wrote:> # Suppose I have a vector: > > myvec = c(1,0,3,0,77,9,0,1,2,0) > > # I want to randomly pick an element from myvec > # where element == 0 > # and print the value of the corresponding index. > > # So, for example I might randomly pick the 3rd 0 > # and I would print the corresponding index > # which is 7, > > # My initial approach is to use a for-loop. > # Also I take a short-cut which assumes myvec is short: > > elm = 1 > while (elm != 0) { > # Pick a random index, (it might be a 0): > rndidx = round(runif(1, min=1, max=length(myvec))) > elm = myvec[rndidx] > if(elm == 0) > print("I am done") > else > print("I am not done") > } > print(rndidx)It's a little easier if you re-arrange your problem statement. This is equivalent: return randomly one index of myvec for which the element of myvec equals 0. A direct implementation of this is sample(which(myvec==0), 1) which(myvec==0) returns a vector of indexes of myvec for which the value of the vector is 0. sample(..., 1) randomly selects one of those.> # If myvec is large and/or contains no zeros, > # The above loop is sub-optimal/faulty.This approach also fails if there is no 0's in the vector. What do you want the result to be when that is the case? If we go with the simple answer of NA, then you can special case that (and wrap it up into a function) OneZeroIndex <- function(myvec) { zeros <- which(myvec==0) if (length(zeros) > 0) { sample(zeros, 1) } else { NA } }> # I suspect that skilled R-people would approach this task differently. > # Perhaps they would use features baked into R rather than use a loop? > [[alternative HTML version deleted]]Please post plain text only. -- Brian S. Diggs, PhD Senior Research Associate, Department of Surgery Oregon Health & Science University