
Untitled
By: a guest on
Jul 17th, 2012 | syntax:
None | size: 1.51 KB | hits: 15 | expires: Never
R: Format output of write.table
# example data to write to text file
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
# left align columns using a tab
write.table(aa,file="c:/users/mark w miller/simple r programs/formatted_tablea.txt", na = 'NA', sep = 't',
row.names = F, col.names = F)
# 1000 110 10 1
# 0 2000 20 2
# 30 300 3000 30000
# increase spacing between columns with two tabs
write.table(aa,file="c:/users/mark w miller/simple r programs/formatted_tableb.txt", na = 'NA', sep = 'tt',
row.names = F, col.names = F)
# 1000 110 10 1
# 0 2000 20 2
# 30 300 3000 30000
# Here is an example of the desired right-aligned output
# format with an intermediate amount of spacing
# (3 spaces vs the 1 or 7 spaces used above):
# 1000 110 10 1
# 0 2000 20 2
# 30 300 3000 30000
# Maybe use cat somehow?
cat(file="c:/users/mark w miller/simple r programs/formatted_tablec.txt", aa, sep=c(' ', ' ', 'n'))
capture.output( print(aa, print.gap=3), file="capture.txt")
C1 C2 C3 C4
[1,] 1000 110 10 1
[2,] 0 2000 20 2
[3,] 30 300 3000 30000
require(MASS)
write.matrix(aa, file="capturemat.txt", sep=" ")
C1 C2 C3 C4
1000 110 10 1
0 2000 20 2
30 300 3000 30000