combine results from a loop in one file

依然范特西╮ 提交于 2019-11-30 09:36:10

问题


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.

Can anyone help me with this?


回答1:


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)
}

Is this what you mean?

EDIT: 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).



来源:https://stackoverflow.com/questions/5404393/combine-results-from-a-loop-in-one-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!