[R] How to override coerion error in 'scan'

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Apr 26 08:28:55 CEST 2005


Kittler, Richard wrote:
> Uwe,
> 
> Thanks. I did find something in the archives about using a custom
> colClass of 'num' with read.csv and using setAs to define a character ->
> num function. From the read.table code this appears to force 'scan' to
> read it as character and then convert it later using 'as'.  I'm not sure
> if there is any advantage in this approach to just reading it using a
> colClass of 'character' and then converting it myself afterward (?)


No, I think you are looking for something fuzzy that does not exist, 
AFAIK. Sorry, I was too short, here.

I mean read it in as character. Then use some function (e.g. using 
regular expressions) to detect what is numeric and what not (e.g. 
"03/15/200523:56:03"), replace the latter by NA and convert to numeric.

Uwe



> --Rich
> 
> Richard Kittler 
> AMD TDG
> 408-749-4099
> 
> -----Original Message-----
> From: Uwe Ligges [mailto:ligges at statistik.uni-dortmund.de] 
> Sent: Saturday, April 23, 2005 6:50 AM
> To: Kittler, Richard
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] How to override coerion error in 'scan'
> 
> 
> Kittler, Richard wrote:
> 
> 
>>I am using 'read.csv' in V2.0.1 to read in a CSV file with the 
>>colClasses option and am getting an error from 'scan' when it 
>>encounters a non-numeric value for a 'numeric' column, i.e.
>>
>> > ds <- read.csv(in_file, nrows=irow, row.names=NULL, 
>>colClasses=zclass,
>>                     comment.char="")
>>  Error in scan(file = file, what = what, sep = sep, quote = quote, 
>>dec = dec, :
>>     "scan" expected a real, got "03/15/200523:56:03"
>>
>>Is there a way to override this and just have it convert those values 
>>to NA? The dataset is large so I would prefer not to have to import 
>>the columns as character and convert them to numeric afterward.
> 
> 
> I think you have to read it in as character - or write your own C-level 
> facility...
> 
> Uwe Ligges
> 
> 
> 
>>--Rich
>>
>>Richard Kittler
>>AMD TDG
>>408-749-4099
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list 
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide! 
>>http://www.R-project.org/posting-guide.html
> 
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list