This seems like it should be a pretty simple question, but I haven't been able to find the answer so I might be searching the wrong keywords. I am currently printing a nicely formatted R dataframe to a file through the method described in the accepted answer to this post, as well as some print options (row.names=FALSE, print.gap=2..., right=FALSE
), and everything is working out fine except that there are newlines between columns.
Essentially, the data is being put into the file like:
column_one column_two
reallylongfoo1 foo2
reallylongfoo3 foo4
... ...
column_three column_four
foo999 foo1000
foo1001 foo1002
... ...
When I want it to be like:
column_one column_two column_three column_four
reallylongfoo1 foo2 foo999 foo1000
reallylongfoo2 foo4 foo1001 foo1002
... ... ... ...
EDIT: While I can't supply any of the data I'm having this problem on since it's proprietary, I can supply some data and code that has the effect I'm describing as suggested by Matt:
mydata <- data.frame(column_one=c("Here is a really long string", "Here is another really long string", "Here is a third really long string"),
column_two=c(15232,-2346.2, 14.3),
column_three=c("Text is also here", "Even more text is also here", "Some final text"),
column_four=c(1, 2, 3))
mydata[,] <- lapply(mydata, function(x) type.convert(as.character(x), as.is=TRUE))
options(max.print=nrow(mydata)*ncol(mydata))
sink("dframe2.txt")
print(mydata, print.gap=2, digits=3, row.names=FALSE, right=FALSE)
sink()
Which outputs in the text file:
column_one column_two column_three
Here is a really long string 15232.0 Text is also here
Here is another really long string -2346.2 Even more text is also here
Here is a third really long string 14.3 Some final text
column_four
1
2
3