[R] Reading in csv with footer

Rolf Turner rolf.turner at xtra.co.nz
Mon Feb 13 01:36:47 CET 2012


On 13/02/12 13:05, Noah Silverman 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.

How clean is clean?

You need to count the number of lines in the file, and then set the nrows
argument of read.csv() to be two less.  (*Two* r.t. one, because of the 
header.)

Counting the lines --- three possibilities that I can see:

     (1) nlines() from the "parser" package
     (2) countLines() from the "R.utils" package
     (3) brute force:
         x <- readLines(<filename>)
         n <- length(x)

Having determined n, do:

         y <- read.csv(<filename>,nrows=n-2)

     cheers,

         Rolf Turner



More information about the R-help mailing list