Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用R中的While循环创建表_R_Loops_While Loop - Fatal编程技术网

用R中的While循环创建表

用R中的While循环创建表,r,loops,while-loop,R,Loops,While Loop,我想用while循环做一张桌子。基本上,我想做一个while循环,其中r的值增加1,并重复这个过程,直到满足不等式。但除此之外,我想将这些值合并到一个包含三列的表中:r的值、w的值和rhs的值(四舍五入到小数点后3位) 我想它应该是这样的,但这只打印出循环结束前r的最后一个值(它在26处结束),结果是“表”只有一行。我想要一个24行的表(因为它从r=2开始) 任何帮助都将不胜感激 也许这会有所帮助: al = 0.10; n = 30; a = 3; b = 5; r = 2; int = 8;

我想用while循环做一张桌子。基本上,我想做一个while循环,其中r的值增加1,并重复这个过程,直到满足不等式。但除此之外,我想将这些值合并到一个包含三列的表中:r的值、w的值和rhs的值(四舍五入到小数点后3位)

我想它应该是这样的,但这只打印出循环结束前r的最后一个值(它在26处结束),结果是“表”只有一行。我想要一个24行的表(因为它从r=2开始)


任何帮助都将不胜感激

也许这会有所帮助:

al = 0.10; n = 30; a = 3; b = 5; r = 2; int = 8; h = (int/2); msE = 19.19
table = function(MSE, V, H, alpha = al, r = 2){
  rhs = h^2*r/((V-1)*MSE)
  w = qf(alpha, V-1, V*(r-1), lower.tail = FALSE)
  g = data.frame(r, round(w, 3), round(rhs, 3))
  gn = data.frame(r, round(w, 3), round(rhs, 3))
  while(w > rhs){
    r = r+1
    rhs = h^2*r/((V-1)*MSE)
    w = qf(alpha, V-1, V*(r-1), lower.tail = FALSE)
    g = data.frame(r, round(w, 3), round(rhs, 3))
    gn <- rbind(gn,g)
  }
return(gn)
}
table(MSE = msE, V = a*b, H = h)
al=0.10;n=30;a=3;b=5;r=2;int=8;h=(int/2);msE=19.19
表=函数(MSE,V,H,α=al,r=2){
rhs=h^2*r/((V-1)*毫秒)
w=qf(α,V-1,V*(r-1),下尾=假)
g=数据帧(r,圆形(w,3),圆形(rhs,3))
gn=数据帧(r,圆形(w,3),圆形(rhs,3))
而(w>rhs){
r=r+1
rhs=h^2*r/((V-1)*毫秒)
w=qf(α,V-1,V*(r-1),下尾=假)
g=数据帧(r,圆形(w,3),圆形(rhs,3))

gn也许这会有帮助:

al = 0.10; n = 30; a = 3; b = 5; r = 2; int = 8; h = (int/2); msE = 19.19
table = function(MSE, V, H, alpha = al, r = 2){
  rhs = h^2*r/((V-1)*MSE)
  w = qf(alpha, V-1, V*(r-1), lower.tail = FALSE)
  g = data.frame(r, round(w, 3), round(rhs, 3))
  gn = data.frame(r, round(w, 3), round(rhs, 3))
  while(w > rhs){
    r = r+1
    rhs = h^2*r/((V-1)*MSE)
    w = qf(alpha, V-1, V*(r-1), lower.tail = FALSE)
    g = data.frame(r, round(w, 3), round(rhs, 3))
    gn <- rbind(gn,g)
  }
return(gn)
}
table(MSE = msE, V = a*b, H = h)
al=0.10;n=30;a=3;b=5;r=2;int=8;h=(int/2);msE=19.19
表=函数(MSE,V,H,α=al,r=2){
rhs=h^2*r/((V-1)*毫秒)
w=qf(α,V-1,V*(r-1),下尾=假)
g=数据帧(r,圆形(w,3),圆形(rhs,3))
gn=数据帧(r,圆形(w,3),圆形(rhs,3))
而(w>rhs){
r=r+1
rhs=h^2*r/((V-1)*毫秒)
w=qf(α,V-1,V*(r-1),下尾=假)
g=数据帧(r,圆形(w,3),圆形(rhs,3))

gn一种稍微不同的方法,消除了对临时数据帧和
rbind()
的需要。在代码中进行了注释

# your parameters
al <- 0.10; n <- 30; a <- 3; b <- 5; int <- 8; h <- (int/2); msE <- 19.19

# your function definition (name changed to avoid confusion / conflict with existing R function)
tabula <- function(MSE, V, H, alpha = al, r = 2)
{
    g <- data.frame( N = 0, W = 1, RHS = 0 )        # initiate data frame, values set
                                                    # so that the while condition is met

    # the while function populates the data frame cell by cell,
    # eliminating the need for an interim data.frame and rbind()
    while( g[ r - 1, "W" ] > g[ r - 1, "RHS" ] )    # check condition in the last data frame row
    {                                               # write values in a new row
        g[ r, "N" ] <- r
        g[ r, "W" ] <- round( qf( alpha, V - 1, V * ( r - 1 ), lower.tail = FALSE ), 3 )
        g[ r, "RHS" ] <- round( h^2 * r / ( ( V - 1 ) * MSE ), 3 )
        r <- r + 1                                  # increment row counter
    }
    return( g[ -1, ] )                              # return the data frame, removing the initial row
}

tabula( MSE = msE, V = a * b, H = h )
#您的参数

al一种稍微不同的方法,消除了对临时数据帧和
rbind()
的需要。在代码中进行了注释

# your parameters
al <- 0.10; n <- 30; a <- 3; b <- 5; int <- 8; h <- (int/2); msE <- 19.19

# your function definition (name changed to avoid confusion / conflict with existing R function)
tabula <- function(MSE, V, H, alpha = al, r = 2)
{
    g <- data.frame( N = 0, W = 1, RHS = 0 )        # initiate data frame, values set
                                                    # so that the while condition is met

    # the while function populates the data frame cell by cell,
    # eliminating the need for an interim data.frame and rbind()
    while( g[ r - 1, "W" ] > g[ r - 1, "RHS" ] )    # check condition in the last data frame row
    {                                               # write values in a new row
        g[ r, "N" ] <- r
        g[ r, "W" ] <- round( qf( alpha, V - 1, V * ( r - 1 ), lower.tail = FALSE ), 3 )
        g[ r, "RHS" ] <- round( h^2 * r / ( ( V - 1 ) * MSE ), 3 )
        r <- r + 1                                  # increment row counter
    }
    return( g[ -1, ] )                              # return the data frame, removing the initial row
}

tabula( MSE = msE, V = a * b, H = h )
#您的参数

与OP的初始尝试相比,您可能想解释是什么使您的版本工作。这正是我需要的。谢谢!您可能想解释是什么使您的版本工作,与OP的初始尝试相比。这正是我需要的。谢谢!
table()
是一个现有的R函数,重新定义它可能不是一个好主意,而是选择一个不同的名称好的调用,我会将它重命名为其他名称(毕竟名称很简单)。
table()
是一个现有的R函数,重新定义它可能不是一个好主意,而是选择一个不同的名称好的调用,我会将它重命名为其他名称(毕竟名称很简单)。