Warning “the condition has length > 1 and only the first element will be used”

后端 未结 1 1693
猫巷女王i
猫巷女王i 2020-12-07 00:23

My R code:

bnc1<-function(maxITR=100000, d=2, l=1){
    counts=0;
    for (i in 1:maxITR){
        x=runif(1,0,pi);
        y=runif(2,0,d/2);
        if (         


        
相关标签:
1条回答
  • 2020-12-07 00:32

    runif returns a vector.

    if takes a single value (not a vector).

    Check the manual for runif, I don't think you are using it right.


    In R, it often makes sense to remove for loops and use vectors instead - for example:

    bnc1<-function(maxITR=100000, d=2, l=1){
        x=runif(maxITR,0,pi);
        y=runif(maxITR,0,d/2);
        counts = sum((l/2*sin(x)) >= y);
        counts/maxITR*d/l
    }
    
    0 讨论(0)
提交回复
热议问题