2

I need to write a text file that looks like this:

Header text More Header text 0 1 1 2 2 3 3 4 More text 0 2 1 4 2 6 3 9 

The numeric values are stored in two data frames.

So I thought about using write.table that does exactly what I need. I was thinking of something like this:

header <- "Header text \n More Header text" df.text1 <- write.table(my.df1, row.names = FALSE, col.names = FALSE) more.text <- "More text" df.text2 <- write.table(my.df2, row.names = FALSE, col.names = FALSE) writeLines(paste(header, df.text1, more.text, df.text2, sep = "\n"), my.file) 

The problem is, write.table writes to a file or connection, and I don't know how to write in a string.

2 Answers 2

3

You can use append = TRUE in write and write.table to append to an existing file.

write(line, file="myfile", append=TRUE) write.table(df, file="myfile", append=TRUE) 
Sign up to request clarification or add additional context in comments.

Comments

1

you can write to my.file directly:

cat(header,"\n",file= my.file) write.table(my.df1, row.names = F, col.names = F,file= my.file,append=T) cat(more.text,"\n",file= my.file,append=T) write.table(my.df2, row.names = F, col.names = F,file= my.file,append=T) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.