我正在運行一個小循環來爲4個組的子集隨機分配一個數字列表(1到30)。我想將循環的輸出(對於4個子集)作爲一行存儲在一個變量中,並將結果存儲在別處。我也收到一些警告,雖然輸出正確顯示在屏幕上。將循環中的輸出存儲爲R中的列表
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
我無法找到此錯誤的原因。請幫忙。謝謝。
'地圖(功能(啓動,停止){樣品(開始:停止,替換= TRUE)},cumsum(c(1,6,9,3)),cumsum(c(6,9,3,12)))' – alistaire
我嘗試初始化'y < - NULL; temp < - sample(...),y
mysqlnew
感謝@alistaire的幫助。它仍然產生四行輸出。我希望單行輸出有兩個原因:(1)樣本量和分組非常大。因此,對於m x n組的單行輸出將會有所幫助。 (2)實際情況下的cumsum值會因此而大於此示例;因此,我希望指定一個列表,可以控制m x n個數據集。再次感謝。 – mysqlnew