Peng Yu
2009-Sep-17 19:23 UTC
[R] How write the same number of elements in the first line as the rest of the file with write.table()?
Hi, The first line has less elements than the rest of 'rownames_colnames.write.table.xls'. I am wondering if there is a way to print an additional '\t' at the beginning of the first line. $ Rscript write.table.R> x=matrix(1:20,nc=2) > rownames(x)=letters[1:10] > colnames(x)=letters[1:2] > write.table(x,"rownames_colnames.write.table.xls",sep='\t') >$ cat rownames_colnames.write.table.xls "a" "b" "a" 1 11 "b" 2 12 "c" 3 13 "d" 4 14 "e" 5 15 "f" 6 16 "g" 7 17 "h" 8 18 "i" 9 19 "j" 10 20 Regards, Peng
David Winsemius
2009-Sep-17 20:11 UTC
[R] How write the same number of elements in the first line as the rest of the file with write.table()?
On Sep 17, 2009, at 3:23 PM, Peng Yu wrote:> Hi, > > The first line has less elements than the rest of > 'rownames_colnames.write.table.xls'. I am wondering if there is a way > to print an additional '\t' at the beginning of the first line. > > $ Rscript write.table.R >> x=matrix(1:20,nc=2) >> rownames(x)=letters[1:10] >> colnames(x)=letters[1:2] >> write.table(x,"rownames_colnames.write.table.xls",sep='\t')If you replace your script file with this it gives the needed padding: #write.table.R x=matrix(1:20,nc=2) rownames(x)=letters[1:10] colnames(x)=letters[1:2] sink("rownames_colnames.write.table.xls") cat("\t") write.table(x,sep='\t') sink()>> > $ cat rownames_colnames.write.table.xls > "a" "b" > "a" 1 11 > "b" 2 12 > "c" 3 13 > "d" 4 14 > "e" 5 15 > "f" 6 16 > "g" 7 17 > "h" 8 18 > "i" 9 19 > "j" 10 20-- David Winsemius, MD Heritage Laboratories West Hartford, CT