0

This question is similar to R: Format output of write.table, and 42-'s answer works except I need to remove row names. capture_output doesn't seem to have an equivalent of row.names = FALSE. I've also tried row.names(aa) <- NULL prior to running capture.output, but that doesn't help.

Is it possible to generate a right-justified text file just like capture.output does below, but with no row names?

aa = matrix(c(1000,110,10,1, 
            0,2000,20,2, 
            30,300,3000,30000), nrow=3, byrow=TRUE,
            dimnames = list(NULL, c("C1", "C2", "C3","C4")))
aa<-as.data.frame(aa)
aa

capture.output( print(aa, print.gap=3), file="capture.txt")

write.fwf doesn't right-justify as desired:

write.fwf(aa, "fwf.txt", rownames=FALSE, sep="\t", quote=FALSE, justify = "right")

Thank you!

6
  • So what exactly is the desired output? Are you trying to generate a fixed-width output file? Commented Jan 21, 2020 at 22:18
  • I want a text file formatted exactly as produced in the example code using capture.output, but without row names. I tried write.fwf with justify = "right", but the output ends up being only "half" right-justified. Adding more spaces with sep="\t\t" didn't fix that issue Commented Jan 21, 2020 at 22:26
  • 2
    Can't you just include row.names=F in your print statement? For example: capture.output( print(aa, print.gap=3, row.names = F), file="capture.txt") Commented Jan 21, 2020 at 22:40
  • Brilliant, thank you! I was putting it in the wrong spot Commented Jan 21, 2020 at 22:52
  • @Ben, do you want to post an answer for me to accept or should I just delete this question? Commented Jan 22, 2020 at 0:17

1 Answer 1

2

To remove the row names, use row.names = FALSE directly within the print statement:

capture.output(print(aa, print.gap=3, row.names = FALSE), file="capture.txt")

Output of capture.txt:

     C1     C2     C3      C4
   1000    110     10       1
      0   2000     20       2
     30    300   3000   30000
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.