将循环的结果合并到一个文件中

问题描述:

我创建一个像这样的循环:

I create a loop like this one:

for (p in 1:nrow(outcomes)) {

    id <- apply(regulationtable, 1, function(i)
        sum(i[1:length(regulationtable)] != outcomes[p,])==0)

    idd <- as.matrix(id)
    test2 = subset(idd, idd[,1]==TRUE)

    result <- as.data.frame(rownames(test2))

    filename = paste("file", p, ".txt")

    write.table(result, filename)
}

每个循环的结果将保存为一个文件.我想将这些结果结合起来,并用所有结果创建一个文件.

The results of every loop will be saved as a file. I want to combine this results and create one file with all the results.

有人可以帮我吗?

使用write.table中的append参数,您可以将行添加到现有文件中,而不是覆盖它们:

With the append argument in write.table you can add lines to an existing file rather than overwriting them:

if (p == 1) 
{
  write.table(result, "file.txt") 
} else
{ 
  write.table(result, "file.txt", append = TRUE, col.names = FALSE)
}

这是你的意思吗?

您可能希望第一次运行来初始化它而不是追加,然后彼此运行以不打印列名(我确实假设每个表的列名都相同).

You might want the first run to initialize it and not append, then each other run to not print the column names ( I do assume these are the same for each table).