我有几个文本文件,我在R中将它们作为数据框读取。当我将这些数据帧放在列表中并在for循环中进行分析时。似乎代码不是逐行运行的。如果我在循环结束时添加一些代码,这可能会导致错误,那么它之前的代码将不会在for循环中运行。有人帮我解释一下吗?
下面是我的代码:
rm(list=ls())
graphics.off()
#cost.seq = 10^seq(2, -3, length = 20)
k=7
error = data.frame(cv = c(1:k))
train.error = data.frame(cv = c(1:k))
report.rows = 3
report.name = "rda"
source("scriptd_stats01_read_feature.R")
source("scriptd_stats01_read_feature_fc.R")
source("scriptd_stats02_cv_functions.R")
multimodal.feature = scale(cbind(spm.vbm, alff, reho, label.fa, tract.fa, tract.md))
feature.list = list(spm.vbm, alff, reho, label.fa, tract.fa, tract.md, fc, multimodal.feature)
library(reshape2)
for (i.feature in 1:length(feature.list)) {
print("loop:")
print(i.feature)
brain.feature = scale(feature.list[[i.feature]])
df.all = cbind(subject.info[,-1], brain.feature)
report = data.frame(group = c("hc vs trauma", "ptsd vs trauma", "hc vs ptsd"), acc=rep(NA, report.rows),sensi=rep(NA,report.rows),speci=rep(NA,report.rows))
report.sd = data.frame(group = c("hc vs trauma", "ptsd vs trauma", "hc vs ptsd"), acc=rep(NA, report.rows),sensi=rep(NA,report.rows),speci=rep(NA,report.rows))
# ---------------------select data for ptsd 0 and 1 :---------------------
df.subset = df.all[df.all$ptsd==0|df.all$ptsd==1,]
print("dimension for subset dataset")
print(dim(df.subset))
print(table(df.subset$ptsd))
x = model.matrix(df.subset$ptsd~., df.subset)
y = df.subset$ptsd
print(dim(x))
set.seed(333)
cv.result = rda.cv.fun(x[,-1], y, k)
result = cv.result[[1]]
train.result = cv.result[[2]]
print(result)
print(train.result)
print("####")
print(colMeans(result, na.rm=T))
report[1,-1] = colMeans(result,na.rm=T)
report.sd[1,-1] = apply(result, 2, function(x)sd(na.omit(x)))
total.report = melt(report, id = ("acc", "sensi", "speci"))
print(total.report)
}
当我在for循环中注释最后两行时,它运行没有错误,并且它打印循环1 2 3到8.但是当我取消注释它们时,之前的代码没有运行(没有打印值)我得到以下错误:
错误:意外','在:
“
total.report = melt(report,id =(“acc”,“
执行暂停
这很奇怪,因为看起来最后两行是在for循环中的前一行之前执行的。