[R] read.csv to read output of system()?

David Winsemius dwinsemius at comcast.net
Sat Dec 12 17:02:58 CET 2009


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"


>
> Marianne
>
> -- 
> Marianne Promberger PhD, King's College London
> http://promberger.info


David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list