2017-05-21 196 views
1

代碼:這個錯誤是什麼意思?

pvalue <- matrix(nrow = 9, ncol = 10000) 

for(i in 1:9) { 
    for(j in 1:10000) { 
    pvalue[i,j]=integrate(dnorm,0,1)  
    } 
} 

錯誤:

Error in pvalue[i, j] <- integrate(dnorm, 0, 1) : number of items to 
replace is not a multiple of replacement length` 
+0

爲什麼想要當R提供了CDF集成密度函數? – Roland

+2

這個錯誤是因爲'length(integrate(dnorm,0,1))'爲5,'length(pvalue [i,j])爲1.你試圖把一個5長的長釘放入1個長洞 –

回答

1

integrate(dnorm,0,1)的輸出是一個列表,而不是數量。

str(integrate(dnorm,0,1)) 

List of 5 
$ value  : num 0.341 
$ abs.error : num 3.79e-15 
$ subdivisions: int 1 
$ message  : chr "OK" 
$ call  : language integrate(f = dnorm, lower = 0, upper = 1) 
- attr(*, "class")= chr "integrate" 

的工作準則是:

pvalue <- matrix(nrow = 9, ncol = 10000) 

for(i in 1:9) { 
    for(j in 1:10000) { 
    pvalue[i,j] <- integrate(dnorm,0,1)$value  
    } 
}