[R] read.table: how to ignore errors?
Sam Steingold
sds at gnu.org
Tue Jan 24 23:00:43 CET 2012
> * William Dunlap <jqhaync at gvopb.pbz> [2012-01-24 21:47:39 +0000]:
>
>> >> Oh, yeah, a reproducible example:
>> >>
>> >> read.csv from
>> >> =====
>> >> a,b
>> >> 1,2
>> >> 3,4
>> >> 5,,6
>> >> 7,8
>> >> =====
>> >> I want to be able to extract the data frame
>> >> a b
>> >> 1 1 1
> The previous line should be '1 1 2', right?
of course, thanks!
>> >> 2 3 4
>> >> 3 7 8
>
> Have you tried using count.fields to remove the lines
> in the file with the wrong number of fields? E.g.,
>
>> tf <- tempfile()
>> cat(c("a,b", "1,2", "3,4", "5,,6", "7,8"), file=tf, sep="\n")
>> # following will fail
>> read.table(tf, sep=",", header=TRUE)
> Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
> line 1 did not have 3 elements
>> # following reads lines with 2 fields only
>> textLines <- readLines(tf)
>> counts <- count.fields(textConnection(textLines), sep=",")
>> read.table(text=textLines[counts == 2], header=TRUE, sep=",")
> a b
> 1 1 2
> 2 3 4
> 3 7 8
sounds good, thanks a lot!
--
Sam Steingold (http://sds.podval.org/) on Ubuntu 11.10 (oneiric) X 11.0.11004000
http://camera.org http://thereligionofpeace.com http://memri.org
http://truepeace.org http://iris.org.il http://jihadwatch.org http://pmw.org.il
My name is Deja Vu. Have we met before?
More information about the R-help
mailing list