'[< - 。data.frame'...替换中的R错误有#个项目,需要# [英] R error in '[<-.data.frame'... replacement has # items, need #
问题描述
设置虚拟数据 - 两个竞争表(结果& count)
result < - data.frame(matrix(runif(40)* 100,nrow = 20))
名称(结果)[1]< - paste(A); name(result)[2]< - paste(B)
count< - data.frame(matrix(runif(40)* 100,nrow = 20))
名称[1]< - 糊(A); name(count)[2]< - paste(B)
n.iter< - 1e3
#Run排列测试
permtest <结果,count){
n < - dim(result)[1]
#print(n)
stat < - function(x,y)abs(diff(range(colSums (x,i){x [i,],cbind(x [,B],x [,A] )[一世, ]; return(x)}
sim< - replicate(n.iter,{i< - runif(n)< 1/2; stat(swap(result,i))swap(count,i) })
result.stat< - stat(result,count)
p.value< - sum(sim> = result.stat)/ length(sim)
return (sim,result.stat,p.value))
}
#随着时间的推移,p值的演化
p.evol< - data.frame()
for(i in 10:dim(result)[1]){
#print(i)
permresults< - permtest(result [1:i,],count [1:i ,])
p.value< - permresults [[3]]
p.evol< - rbind(p.evol,c(i,p.value,1-p.value))
}
colnames(p.evol)< - c(day,p.value,conf)
dev.new()
plot .evol [,1],p.evol [,3],type =b,xlab =Day,ylab =Percentage,main =Evolution of Confidence)
问题是,虽然有时它没有问题,但大多数时候我得到错误在'[< .data.frame'('* tmp *',i,,value = numeric(0)):替换有0项,需要24
。使用 options(error = traceback)
我得到这里的输出,我不明白:
f(ngettext(m,replacement has%d item,need%d,
replacement has%d items,need%d),m,n * p),domain = NA)
16:`[< - 。data.frame`(`* tmp *`,i,,value = numeric(0))在errortest.R#16
15:`[< ; -`(`* tmp *`,i,,value = numeric(0))在errortest.R#16
14:swap(result,i)
13:is.data.frame x)
12:colSums(x)
11:diff(range(colSums(x)/ colSums(y)))在errortest.R#15
10:stat(swap (0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L) ,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
0L,0L,0L,0L,0L, ,0L,0L,0L,0L,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L,0L,0L,0L,0L,0L,0L,0L,0L, ,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L, ,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L, ,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
0L,0L,0L,0L,0L,0L, ,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
0L,0L,0L, 0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L, ,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
0L,0L,0L,0L, 0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,
0L, 0L,0L,0L,0L,0L,
0L,0L,0L,0L,0L,0L,0L,0L,0L,0L)[[42L]],...)
8: lapply(X = X,FUN = FUN,...)
7:sapply(integer(n),eval.parent(substitute(function(...)expr)),
simplified = )
6:replicate(n.iter,{
i< - runif(n)< $ {$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ,count [1:i,])在errortest.R#27
4:eval(expr,envir,enclosure)
3:eval(ei,envir)
2:withVisible (ei,envir))
1:source(errortest.R,echo = F)
特别令人困惑的是,它有时是这怎么可能?我也注意到,当我取消评论 print(n)
和 print(i)
似乎使它工作更频繁,尽管它们在没有评论和工作时可能会失败。感谢您的帮助!
当您不幸运时,此错误会弹出, i< ; - runif(n)< 1/2
仅由 FALSE
组成,即不会发生排列。您需要在交换
函数中添加支票来解决此问题。
I am new to R and this one is beyond me. The script below uses two dummy tables (result and count) each with two columns (A and B). I'm running permutations tests to compare the results from A and B. Specifically, I'm looking at result/count for A and B. Both result and count have 20 rows and I've written a loop to run a permutation test for the first 10 rows of each, then the first 11, then 12, up to 20. When it works, which it does on occasion, I get a pretty graph at the end.
#Set up the dummy data - two competing tables (result & count)
result <- data.frame(matrix(runif(40)*100, nrow=20))
names(result)[1] <- paste("A"); names(result)[2] <- paste("B")
count <- data.frame(matrix(runif(40)*100, nrow=20))
names(count)[1] <- paste("A"); names(count)[2] <- paste("B")
n.iter <- 1e3
#Run a permutation test
permtest <- function(result, count) {
n <- dim(result)[1]
# print(n)
stat <- function(x, y) abs(diff(range(colSums(x)/colSums(y))))
swap <- function(x, i) { x[i, ] <- cbind(x[, "B"], x[, "A"])[i, ]; return (x) }
sim <- replicate(n.iter, { i <- runif(n) < 1/2; stat(swap(result, i), swap(count, i)) })
result.stat <- stat(result, count)
p.value <- sum(sim >= result.stat) / length(sim)
return(list(sim, result.stat, p.value))
}
#Compute evolution of p-values over time
p.evol <- data.frame()
for (i in 10:dim(result)[1]) {
# print(i)
permresults <- permtest(result[1:i,], count[1:i,])
p.value <- permresults[[3]]
p.evol <- rbind(p.evol, c(i, p.value, 1-p.value))
}
colnames(p.evol) <- c("day", "p.value", "conf")
dev.new()
plot(p.evol[,1],p.evol[,3], type="b", xlab="Day",ylab="Percentage", main="Evolution of Confidence")
The problem is that while sometimes it runs no problem, most of the time I get Error in '[<-.data.frame'('*tmp*', i, , value = numeric(0)) : replacement has 0 items, need 24
. With options(error=traceback)
I get the output here, which I am not understanding:
f(ngettext(m, "replacement has %d item, need %d",
"replacement has %d items, need %d"), m, n * p), domain = NA)
16: `[<-.data.frame`(`*tmp*`, i, , value = numeric(0)) at errortest.R#16
15: `[<-`(`*tmp*`, i, , value = numeric(0)) at errortest.R#16
14: swap(result, i)
13: is.data.frame(x)
12: colSums(x)
11: diff(range(colSums(x)/colSums(y))) at errortest.R#15
10: stat(swap(result, i), swap(count, i)) at errortest.R#17
9: FUN(c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0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L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)[[42L]], ...)
8: lapply(X = X, FUN = FUN, ...)
7: sapply(integer(n), eval.parent(substitute(function(...) expr)),
simplify = simplify)
6: replicate(n.iter, {
i <- runif(n) < 1/2
stat(swap(result, i), swap(count, i))
}) at errortest.R#17
5: permtest(result[1:i, ], count[1:i, ]) at errortest.R#27
4: eval(expr, envir, enclos)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))
1: source("errortest.R", echo = F)
What is particularly puzzling is that it works sometimes! How is this possible? I've also noticed that when I un-comment print(n)
and print(i)
it seems to make it work more frequently, although it can fail when they are not commented and work when they are. Thanks in advance for the help!
This error pops up when you're unlucky and i <- runif(n) < 1/2
consists only of FALSE
, i.e. no permutations happen. You need to add a check in the swap
function to fix this problem.
这篇关于'[< - 。data.frame'...替换中的R错误有#个项目,需要#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!