美文网首页磕盐——从入门到自闭
『R脚本练习』写了一个有趣的函数~

『R脚本练习』写了一个有趣的函数~

作者: ShawnMagic | 来源:发表于2020-09-17 19:54 被阅读0次

    话不多说,直接上代码

    getRandomNum = function(x,min,q25,q50,q75,max){
      if (x%%2 == 0 & (x/2)%%2 == 0) {
        tmp1 = runif(x/4-2,min,q25-1)
        tmp2 = runif(x/4-2,q25+1,q50-1)
        tmp3 = runif(x/4-2,q50+1,q75-1)
        tmp4 = runif(x/4-2,q75+1,max)
        y = c(min,tmp1,q25-1,q25+1,tmp2,q50-1,q50+1,tmp3,q75-1,q75+1,tmp4,max)
      } else if(x%%2 == 0 & (x/2)%%2 != 0){
        tmp1 = runif(ceiling(x/4-2),min,q25)
        tmp2 = runif(ceiling(x/4-2),q25,q50-1)
        tmp3 = runif(ceiling(x/4-2),q50+1,q75)
        tmp4 = runif(ceiling(x/4-2),q75,max)
        y = c(min,tmp1,q25,tmp2,q50-1,q50+1,tmp3,q75,tmp4,max)
      } else if(x%%2 != 0 & (x-1)%%4 == 0){
        tmp1 = runif((x-1)/4-1,min,q25)
        tmp2 = runif((x-1)/4-1,q25,q50)
        tmp3 = runif((x-1)/4-1,q50,q75)
        tmp4 = runif((x-1)/4-1,q75,max)
        y = c(min,tmp1,q25,tmp2,q50,tmp3,q75,tmp4,max)
      } else if(x%%2 != 0 & (x-1)%%4 != 0){
        tmp1 = runif(ceiling((x-1)/4-2),min,q25-1)
        tmp2 = runif(ceiling((x-1)/4-2),q25+1,q50-1)
        tmp3 = runif(ceiling((x-1)/4-2),q50+1,q75-1)
        tmp4 = runif(ceiling((x-1)/4-2),q75+1,max)
        y = c(min,tmp1,q25-1,q25+1,tmp2,q50,tmp3,q75-1,q75+1,tmp4,max)
      }
      return(y)
    }
    z = getRandomNum(x = 5,min = 2230,q25 = 18123,q50 = 52213,q75 = 78312,max = 234234123)
    quantile(z)
    fivenum(z)
    

    相关文章

      网友评论

        本文标题:『R脚本练习』写了一个有趣的函数~

        本文链接:https://www.haomeiwen.com/subject/nqkryktx.html