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

From: Joris Meys <jorismeys_at_gmail.com>
Date: Sun, 13 Jun 2010 15:23:42 +0200

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

______________________________________________
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.
Received on Sun 13 Jun 2010 - 13:25:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 13 Jun 2010 - 13:30:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive