【发布时间】:2014-02-23 19:35:05
【问题描述】:
我一直在对代码进行故障排除,但被卡住了。我试图在 R 的 for 循环中重复运行一个函数,同时附加到一个向量。我的代码如下:
densities = rep(NA, length(seq(0, 1, by = 0.01)))
count = 1
for (t in seq(0, 1, by = 0.01)) {
applyThreshold(t, densities, count)
}
applyThreshold = function(t, densities, count){
tMatrix = corMatrix
tMatrix[corMatrix < t] = 0
tMatrix[corMatrix >= t] = 1
nVector = c()
for (n in 1:dim(tMatrix)[1]){
nSum = sum(tMatrix[,n])
if (nSum == 0){
nVector = c(nVector, n)
}
}
if (length(nVector) >0){
tMatrix = tMatrix[,-(nVector)]
tMatrix = tMatrix[-(nVector),]
}
E = sum(tMatrix)/2
V = dim(tMatrix)[1]
densities[count] = 2*E/(V*(V-1))
count = count + 1
}
但是,在运行此命令结束时,我的密度和计数尚未更新。我觉得这是一个简单的修复,但我一直在查看 R 中函数的文档,但仍然卡住了。任何建议将不胜感激!
【问题讨论】:
-
我会去掉函数中的前14行代码,设置E = 2,V = 3,用seq(0, 1, by = 0.2)代替0.01,把答案贴出来给你想。一旦你让简化的函数正常工作,然后重新添加前 14 行代码并尝试使用 by = 0.01。