On 12/12/09 11:02, David Winsemius wrote:
> 
> On Dec 12, 2009, at 7:54 AM, Marianne Promberger wrote:
> 
> > Dear list,
> >
> > I have a file that is comma delimited but contains some erroneous
> > non-delimiter commas. I would like to replace these commas with
> > semicolons and then read the correct file into R as a data frame.
> >
> > I want to do this from within R, without changing the original data
> > file.
> >
> > My current idea of how to do this would be to use system("sed ...")
> > and feed the result to read.csv(), but I cannot figure out how to
> > combine the two.
> >
> > Minimal example:
> >
> > system("echo \"one,two,three\" > file.csv") # create mockup file
> > read.csv(file=system("sed -e 's/,/;/' file.csv"))  # this does not  
> > work
> >
> > I think the answer must be in ?connections, maybe pipe() but I have
> > fiddled with these and cannot figure it out.
> 
> You need to figure out how to do multiple replacements unless it is  
> only the first comma that you are targeting:
> 
>  > readLines(pipe("sed -e 's/,/;/' ~/file.csv"))
> [1] "one;two,three"

So this should be (inside the quotes)

sed -e 's/,/;/g' ~/file.csv

But this wasn't the problem.

So how about this:

gsub(readLines("file.csv"),",",";")

This will work for a one-line file, but it will require modification
for many lines.

(But I'm not sure how to distinguish commas that should be there and
those that should not.)

Jon

______________________________________________
R-help@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.

Reply via email to