[R] How to output text to sink without initial line number [1], and data frame line by line without column names

Nevil Amos nevil.amos at gmail.com
Sun Jun 13 08:21:12 CEST 2010


I want to output a text file assembeld from various soruces within r ( 
actually as a genepop file)

the output should be formatted     line 1 "text comment"
                                                    line 2:n  selected 
column names from data frame
                                                     line n+1on lines of 
selected columns  from data frame one row at a time


I have the following code, but cannot see how to remove the line numbers 
and omit column names form the line by line data frame output

col1<-c(2,45,67)
col2<-c("a","B","C")
col3<-c(234,44,566)
mydf<-as.data.frame(cbind(col1,col2,col3))
n<-ncol(mydf)
nr<-nrow(mydf)
sink("test.txt")

print("I will be including text of various sorts in this file so cannot 
use print table or similar command")
for (i in 1:n){
print(colnames(mydf[i]),quote=F) }
for (j in 1:nr){
print(mydf[j,c(2:n)],quote=F,row.names=F)}
sink()

The test.txt contains:

[1] "I will be including text of various sorts in this file so cannot 
use print table or similar command"
[1] col1
[1] col2
[1] col3
  col2 col3
     a  234
  col2 col3
     B   44
  col2 col3
     C  566

what I would like in test.txt  is:

"I will be including text of various sorts in this file so cannot use 
print table or similar command"
col1
col2
col3
  a  234
  B   44
  C  566

Many thanks

Nevil Amos



More information about the R-help mailing list