我是R新手,正在嘗試創建不同大小組的自舉重採樣的新數據框。我的數據幀有6個變量和一個組名,有128組不同的Ns。這是我的數據的一個例子:(!謝謝)來自這個問題拉for循環for循環?在R
head(PhenoM2)
ID Name PhenoNames Group HML RML FML TML FHD BIB
1 378607 PaleoAleut PaleoAleut 1 323.5 248.75 434.50 355.75 46.84 NA
2 378664 PaleoAleut PaleoAleut 1 NA 238.50 441.50 353.00 45.83 277.0
3 378377 PaleoAleut PaleoAleut 1 309.5 227.75 419.00 332.25 46.39 284.0
4 378463 PaleoAleut PaleoAleut 1 283.5 228.75 397.75 331.00 44.37 255.5
5 378602 PaleoAleut PaleoAleut 1 279.5 230.00 393.00 329.50 45.93 265.0
6 378610 PaleoAleut PaleoAleut 1 307.5 234.25 419.50 338.50 43.98 271.5
- - bootstrap resampling for hierarchical/multilevel data並從別人採取了一些意見,我寫的代碼:
resample.M <- NULL
for(i in 1000){
groups <- unique(PhenoM2$"Group")
for(ii in 1:128)
data.i.ii <- PhenoM2[PhenoM2$"Group"==groups[ii],]
resample.M[i] <- data.i.ii[sample(1:nrow(data.i.ii),replace=T),]
}
不幸的是,這給了我警告:
In resample.M[i] <- data.i.ii[sample(1:nrow(data.i.ii), replace = T),:
number of items to replace is not a multiple of replacement length
我的理解,因爲每個128組都有不同的N和沒有它是1000多,我把resample.M [I]嘗試和積累所有的1000倍[R將128個組的樣本集合到一個數據庫中,我很確定問題出在這裏。
幾乎所有我讀過的循環示例都創建了一個向量數據庫 - 數字(1000) - 然後插入信息,但由於我想要所有的數據(包括因子,整數和數字)這不起作用。我試圖製作一個矩陣來放入信息(數據框中有2187個獨特的個人):
resample.M <- matrix(ncol=2187000,nrow=10)
但它給了我同樣的警告。
所以,既然我敢肯定,我失去了一些東西基本在這裏,我有三個問題:
我怎樣才能得到這個代碼重新採樣的所有組(含更換,並根據他們的個人NS) ?
我怎樣才能得到這個代碼重複這個重採樣1000倍?
我怎樣才能得到每個組的resamples到同一個數據庫?
非常感謝您的洞察力和專業知識!
解決您收到的警告。通過輸入'resample.M [i]'您正在訪問第i個元素。行訪問由'resample.M [i,]'和列訪問'resample.M [,i]'完成。 – Brouwer