[R] How write the same number of elements in the first line as the rest of the file with write.table()?

David Winsemius dwinsemius at comcast.net
Thu Sep 17 22:11:18 CEST 2009


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




More information about the R-help mailing list