R repeat function until condition met

前端 未结 4 2007
别那么骄傲
别那么骄傲 2020-12-23 22:32

I am trying to generate a random sample that excludes certain \"bad data.\" I do not know whether the data is \"bad\" until after I sample it. Thus, I need to make a rando

4条回答
  •  感情败类
    2020-12-23 23:19

    You can just select the rows to sample directly like so (just 5):

    > df <- data.frame(NAME=c(rep('Frank',10),rep('Mary',10)), SCORE=rnorm(20))
    > df[sample(which(df$SCORE>0), 5),]
    
    
     NAME     SCORE
    14  Mary 1.0858854
    10 Frank 0.7037989
    16  Mary 0.7688913
    5  Frank 0.2067499
    17  Mary 0.4391216
    

    this is without replacement, for bootstrap put in replace=T.

提交回复
热议问题