Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
764 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...