-2

我无法理解这个错误:

Error in file(paste(out.dir, paste("OutSam", i, "M", M, ".txt", sep = ""),  : 
  invalid 'description' argument

为什么和代码对我来说似乎是正确的。请问可以给我解释一下吗?我想要做的是每次我有不同的循环和不同的样本和迭代次数时保存输出。

M1 <- function(n){
M1list <- list()
x = rnorm(n)
av <- mean(x)
M1list$av <- av
return(M1list)
}
M2 <- function(n){
M2list <- list()
x = rnorm(n)
va <- var(x)
M2list$va <- va
return(M2list)
}
get.pred <- function(asim,ModelFun,M){
test <- list()
dat <- rnorm(asim)
if(M==1){
Model <- ModelFun(asim)
Ass <- matrix(NA,nrow=asim,ncol=1)
Y <- Model$av
Y <- Y+dat
Ass[,1] <- Y
}
else{
Model <- ModelFun(asim)
Ass <- matrix(NA,nrow=asim,ncol=1)
Y <- Model$va
Y <- Y+dat
Ass[,1] <- Y
}
test$Size <- asim
test$Ass <- Ass
return(test)
}


out.dir <- "C:/Documents and Settings/Fsaid/Desktop/R Output"
Ass.dir <- "C:/Documents and Settings/Fsaid/Desktop/R Output/Assessment"
set.pred <- function(asim,ModelFun){
for(i in asim){
for(mf in ModelFun){
M <- as.integer(gsub("\\M", "", quote(mf)))
zz <- file(paste(out.dir,paste("OutSam",i,"M",M,".txt",sep=""),sep="/"))
Ass <- file(paste(Ass.dir,paste("Asse",i,"M",M,".txt",sep=""),sep="/"))  
Pred <- get.pred(asim=i,ModelFun=mf,M=M)
out <- capture.output(Pred)
cat(out,file=zz,sep="\n",append=TRUE)
write.table(test$Ass,quote=F,file=Ass,row.names=T,col.names=NA,sep="\t")
close(zz)
close(Ass)
     }
    }
   }
Set <- set.pred(asim=c(300,500,700,900,1100),ModelFun = c(M1,M2))

drive <- "E:"
Set <- set.pred(asim=c(300,500,700,900,1100),ModelFun = c(M1,M2)) 
4

1 回答 1

1

如果我只是这样做,这里没有错误

 out.dir <- 'c:/foo and bar/subdir/'
 M <- 'MMM'
 file(paste(out.dir, paste("OutSam", i, "M", M, ".txt", sep = ""))))

结果是:

  description                          class 
"c:/foo and bar/subdir/ OutSam100MMMM.txt"                         "file" 
                      mode                           text 
                       "r"                         "text" 
                    opened                       can read 
                  "closed"                          "yes" 
                 can write 
                     "yes" 

您可能需要仔细查看您的M实际情况。

于 2012-10-02T19:25:04.010 回答