2013-02-06 22 views
2

我有一個數據幀,看起來像這樣的不同數目:地層()從「採樣」返回錯誤:參數意味着行

'data.frame': 1090 obs. of 8 variables: 
$ id   : chr "INC000000209241" "INC000000218488" "INC000000218982" "INC000000225646" ... 
$ service.type : chr "Incident" "Incident" "Incident" "Incident" ... 
$ priority  : chr "Critical" "Critical" "Critical" "Critical" ... 

我命令數據,如下所示:

data <- data[order(data$priority),] 

我一直在改變優先因素等,但無論我怎麼努力,當我嘗試運行下面:

s = strata(data,c("priority"),size=c(0,0,1,5)) 

我ALWA YS收到以下錯誤:

Error in data.frame(..., check.names = FALSE) : 
    arguments imply differing number of rows: 0, 1 

我試着調試功能,看看我是否能告訴爲什麼會引發錯誤(但我不能讓代碼的意義上)。在執行strata()函數的這個階段引發錯誤:

debug: r = cbind(r, i) 

非常感謝您的幫助!

+0

您沒有提供可重複的示例,因此很難幫助您? 「strata」是來自哪個包,'生存'? – agstudy

+1

@agstudy,它來自「抽樣」包 – A5C1D2H2I1M1N2O1R2T1

回答

4

問題在於你試圖從一些等於零的組中設置樣本大小。取而代之的是,在採樣前將原始數據分組

在這裏,我們重現您的問題。

library(sampling) 
data(swissmunicipalities) 
length(table(swissmunicipalities$REG)) # We have seven strata 
# [1] 7 

# Let's take two from each group 
strata(swissmunicipalities, 
     stratanames = c("REG"), 
     size = rep(2, 7), 
     method="srswor") 
#  REG ID_unit  Prob Stratum 
# 93  4  93 0.011695906  1 
# 145 4  145 0.011695906  1 
# 2574 1 2574 0.003395586  2 
# 2631 1 2631 0.003395586  2 
# 826 3  826 0.006230530  3 
# 1614 3 1614 0.006230530  3 
# 583 2  583 0.002190581  4 
# 1017 2 1017 0.002190581  4 
# 1297 5 1297 0.004246285  5 
# 2535 5 2535 0.004246285  5 
# 342 6  342 0.010752688  6 
# 347 6  347 0.010752688  6 
# 651 7  651 0.008163265  7 
# 2471 7 2471 0.008163265  7 

# Let's try to drop the first two groups. Oops... 
strata(swissmunicipalities, 
     stratanames = c("REG"), 
     size = c(0, 0, 2, 2, 2, 2, 2), 
     method="srswor") 
# Error in data.frame(..., check.names = FALSE) : 
# arguments imply differing number of rows: 0, 1 

讓我們子集,然後再試一次。

swiss2 <- swissmunicipalities[!swissmunicipalities$REG %in% c(1, 2), ] 
table(swiss2$REG) 
strata(swiss2, 
     stratanames = c("REG"), 
     size = c(2, 2, 2, 2, 2), 
     method="srswor") 
#  REG ID_unit  Prob Stratum 
# 58  4  58 0.011695906  1 
# 115 4  115 0.011695906  1 
# 432 3  432 0.006230530  2 
# 986 3  986 0.006230530  2 
# 1007 5 1007 0.004246285  3 
# 1150 5 1150 0.004246285  3 
# 190 6  190 0.010752688  4 
# 497 6  497 0.010752688  4 
# 1049 7 1049 0.008163265  5 
# 1327 7 1327 0.008163265  5 
+0

了不起,非常感謝!我不知何故偶然發現了這一點,但真的很感謝你花時間澄清這一點,並將按照您所建議的那樣使用優雅的子集方法!再次感謝 – dreamwalker

+0

(自我提升警示):@dreamwalker,您可能還想查看我爲'data.frame'分層抽樣編寫的函數。它被稱爲「分層」,可作爲[此軟件包]的一部分(https://github.com/mrdwab/mrdwabmisc)。它可以讓你像沒有任何錯誤一樣使用'stratified(swissmunicipalities,「REG」,c(0,0,2,2,2,2,2))'等語法,可能是因爲原來的' data.frame發生。 – A5C1D2H2I1M1N2O1R2T1

相關問題