Skip to content
Prev 276086 / 398506 Next

Vectorize 'eol' characters

Dear David,

Thank you for your follow-up and extra-suggestion.

Your additional stab at my problem indeed looks like it'd work too; as I previously wrote, I had already devised a work-around but was nonetheless left wondering whether a more elegant and compact solution was still escaping my knowledge.

Thank you as well to all R-helpers who've provided me with feedback and insights; all the best,


--
Dr Stefano Conti
Statistics Unit (room #2A19)
Health Protection Services
HPA Colindale
61 Colindale Avenue
London NW9 5EQ, UK
tel: +44 (0)208-3277825
fax: +44 (0)208-2007868



-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net]
Sent: Tue 01/11/2011 16:27
To: Stefano Conti
Cc: Prof Brian Ripley; r-help at r-project.org
Subject: Re: [R] Vectorize 'eol' characters
On Nov 1, 2011, at 5:18 AM, Stefano Conti wrote:

            
(Sounds like what I arrived at.)
Here is what I cobbled together to as a work-around to get rid of the  
separator before your pseudo-EOL. Seems that some parts of it might  
apply in your situation, but I'm not sure it's any better than what  
you have constructed. Perhaps it will give you further ideas about how  
to encapsulate behaviors you desire in a function:

# Take a dataframe:

dfrm <- data.frame(a=rnorm(5), b=rnorm(5), cc =paste("tt", 1:5) )

# You can remove the last separator (in this example a comma) before the
# varying "eol string" (in this example "tt") as long as it is unique  
on each line.

sub(',tt', 'tt', capture.output(write.table(dfrm , file="", sep=",",  
quote=FALSE)) )

# Then this can be written to file with:

mod.df <- sub(',tt', 'tt', capture.output(
                        write.table(dfrm , file="", sep=",",
                                     col.names=FALSE, quote=FALSE)) )
writeLines(mod.df, con=file("test.txt") )

(It will still have a regular "eol" == "\n" unless you change that it  
the writeLines call.)