0

I am trying to output a dataframe in R to a .txt file. I want the .txt file to ultimately mirror the dataframe output, with columns and rows all aligned. I found this post on SO which mostly gave me the desired output with the following (now modified) code:

gene_names_only <- select(deseq2_hits_table_df, Gene, L2F)
colnames(gene_names_only) <- c()

capture.output(
  print.data.frame(gene_names_only, row.names=F, col.names=F, print.gap=0, quote=F, right=F),
  file="all_samples_comparison_gene_list.txt"
)

The resultant output, however, does not align negative and positive values. See:

Non-aligned values

I ultimately want both positive and negative values to be properly aligned with one another. This means that -0.00012 and 4.00046 would have the '-' character from the prior number aligned with the '4' of the next character. How could I accomplish this?

Two other questions:

  1. The output file has a blank line at the beginning of the output. How can I change this?

  2. The output file also seems to put far more spaces between the left column and the right column than I would want. Is there any way I can change this?

xfrostyy
  • 37
  • 5

1 Answers1

1

Maybe try a finer scale treatment of the printing using sprintf and a different format string for positive and negative numbers, e.g.:

> df = data.frame(x=c('PICALM','Luc','SEC22B'),y=c(-2.261085123,-2.235376098,2.227728912))

> sprintf('%15-s%.6f',df$x[1],df$y[1])
[1] "PICALM         -2.261085"

> sprintf('%15-s%.6f',df$x[2],df$y[2])
[1] "Luc            -2.235376"

> sprintf('%15-s%.7f',df$x[3],df$y[3])
[1] "SEC22B         2.2277289"

EDIT: I don't think that write.table or similar functions accept custom format strings, so one option could be to create a data frame of formatted strings and the use write.table or writeLines to write to a file, e.g.

dfstr = data.frame(x=sprintf('%15-s', df$x),
                   y=sprintf(paste0('%.', 7-1*(df$y<0),'f'), df$y))

(The format string for y here is essentially what I previously proposed.) Next, write dfstr directly:

write.table(x=dfstr,file='filename.txt',
            quote=F,row.names=F,col.names=F)  
njp
  • 620
  • 1
  • 3
  • 16
  • This looks like a good solution but I will be printing many many lines in the output. I suppose I could create a function to do so but it would entail iterating through the dataframe and outputting the information to a .txt file line-by-line (using something basic like found here: https://stackoverflow.com/questions/2470248/write-lines-of-text-to-a-file-in-r). Is there a way that could potentially implement what you are suggesting without the computational expense of iteration? – xfrostyy Jul 23 '21 at 02:59