写入时不附加 R 的数据

Data not appending with R when writing

我正在将输出写入文件,但数据没有附加。它每次都创建最后一行。代码如下

op <- function(crime) {
filename <- paste(crime,".txt")
fileconn <- file(filename)
cat(nthecrime, file=fileconn, sep="  ",append=TRUE)
#write(nthecrime,file=fileconn, ncolumns=9, append=TRUE,sep="\t")
close(fileconn)
}

每次我调用上面的行而不是追加时,cat 和 write 都会创建一个新文件。我错过了什么?

问候 象头神

来自 ?cat 帮助:

append logical. Only used if the argument file is the name of file (and not a connection or "|cmd"). If TRUE output will be appended to file; otherwise, it will overwrite the contents of file.

您应该使用 filename,而不是 fileconn。尝试

cat(nthecrime, file=filename, sep="  ",append=TRUE)