0

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          
  • 1
    please provide actual data that we can use to reproduce the issue. – Matt W. Jul 23 '18 at 19:43
  • You should be able to control that with the `width` option. See e.g. [here](https://stackoverflow.com/questions/1172485/how-to-increase-the-number-of-columns-using-r-in-linux). – Mikko Marttila Jul 23 '18 at 20:12
  • @MikkoMarttila worked exactly how I needed it to! Thanks! Rip I feel kind of dumb for not having found that post... –  Jul 23 '18 at 20:21

0 Answers0