我正在运行一个小循环,将一个数字列表(1到30)随机分配给4个组的子集。我想将循环的输出(对于4个子集)作为一行存储在一个变量中,并在其他地方使用结果。我也收到一些警告,但输出正确显示在屏幕上。
list = as.vector(c(6, 9, 3, 12)
start <- 1
end <- 6
i <- 1
while(i<=list){
print(sample(start:end, replace=T))
start <- start+list[i]
end <- end + list[i+1]
i <- i+1
}
[1] 3 5 6 1 5 6
[1] 9 13 12 7 11 12 14 11 14
[1] 16 17 17
[1] 28 22 26 21 28 26 22 28 26 30 21 19
Error in start:end : NA/NaN argument
In addition: Warning messages:
1: In while (i <= list) { :
the condition has length > 1 and only the first element will be used
2: In while (i <= list) { :
the condition has length > 1 and only the first element will be used
3: In while (i <= list) { :
the condition has length > 1 and only the first element will be used
4: In while (i <= list) { :
the condition has length > 1 and only the first element will be used
5: In while (i <= list) { :
the condition has length > 1 and only the first element will be used
我无法找到此错误的原因。请帮忙。谢谢。
答案 0 :(得分:0)
使用for循环比使用while循环更好,当我们使用seq函数时不需要子变量i变量
// and you need a %n on the end to make 3 lines.
System.err.format("1st Line %nPrints At 3rd Line,Shouldn't this be In 2nd Line%n");
System.err.flush(); // <-- only needed if the previous write doesn't have
// an implicit flush(); newline (%n) does.
System.out.println("Shouldn't this be the third line,prints at 2nd line");