[R] Reading in csv with footer

Steve Lianoglou mailinglist.honeypot at gmail.com
Mon Feb 13 01:23:33 CET 2012


Hi,

On Sun, Feb 12, 2012 at 7:05 PM, Noah Silverman <noahsilverman at ucla.edu> wrote:
> Hi,
>
> I have a CSV file that is formatted well, except that the last line is a "summary" not is CSV format.
>
> Toy example:
>
> label_1, label_2, label_3
> 1,2,3
> 3,2,4
> 2,3,4
> Total Rows: 3
>
> When I try to import this into R with:  d <- read.table("foo.csv", header=T, sep=",")
> It fails to import properly because of the last line.
>
> Currently, I have a shell script that strips the last line from the file, then it imports to R cleanly.  I don't like this extra layer of processing.
>
> Is there a way to import something like this cleanly in R.

This is arguably the file's problem, so I'm not sure how many "clean"
solutions you will find, but one thing you can do is perhaps count the
number of lines in the file, then set the `nrows` argument in your
call to read.table to be 1 less than that.

How to count the lines, though? Assuming you're on *nix (or have
cygwin), you can do something like:

N <- system("wc -l /path/to/file.csv")

(you'll have to do some parsing on N)

You could also first call `readLines` and find the length of the
result, but this would require you to read the file twice, so ... pick
your poison.

Too bad the person authoring the file doesn't prefix those lines with
some comment character ...

-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the R-help mailing list