[R] How to output text to sink from data frame line by line without column names

Joris Meys jorismeys at gmail.com
Sun Jun 13 15:23:42 CEST 2010


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

cat("I will be including text of various sorts in this file so cannot
use print table or similar command")
for (i in 1:n){
cat(colnames(mydf[i])," ")
cat("\n") }
for (j in 1:nr){
cat(unlist(mydf[j,c(2:n)])," ")
cat("\n")}
#sink()

Cheers
Joris

On Sun, Jun 13, 2010 at 8:41 AM, Nevil Amos <nevil.amos at gmail.com> wrote:
> OK I see how to remove the line numbers[1] etc by using cat instead of
> print, but cannot work out how to remove the column names from the data
> frame output
>
> On 13/06/2010 4:21 PM, Nevil Amos wrote:
>>
>> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php



More information about the R-help mailing list