[R] Understanding read.csv error message

Ben Tupper btupper @end|ng |rom b|ge|ow@org
Sat Aug 18 14:00:16 CEST 2018


Hi,

I would be tempted, as a start, to read the entire file in as rows of text, split each line by the expected delimiter, and then count the number of elements each split line yields.  Once you know each row splits into the expected number of 

txt <- "name,easting,northing,elev,sampdate,prcp
Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-01,0.59
Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-02,0.08
Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-03,0.1
Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-04,0
Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-05,0
Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-06,0.02
Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-07,0.05
Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-08,0.1
Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-09,0
Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-10,0.02"

txtCon <- textConnection(txt)
x <- readLines(txtCon)
close(txtCon)

n <- sapply(strsplit(x, ",", fixed = TRUE), length)
table(n)

If any have a different length then investigate that/those line(s).  If they all have the same length then it likely isn't about the delimiter.

Cheers,
Ben


 
> On Aug 18, 2018, at 5:05 AM, peter dalgaard <pdalgd using gmail.com> wrote:
> 
> What Don said, and also notice that the error is not about anything having value 0, it is about replacing something with something of _length_ 0. It is not obvious where that happens, sometimes a traceback() can give a clue, but probably Don is right that the issue is that there is something not quite CSV in the file. 
> 
> One further idea is to read using colClasses="character" and see if that actually gives you 6 columns and then afterwards try and convert each column to the appropriate type.
> 
> -pd
> 
>> On 18 Aug 2018, at 00:08 , Rich Shepard <rshepard using appl-ecosys.com> wrote:
>> 
>> I have a data file, 'precip_projected.csv,' that starts like this:
>> 
>> name,easting,northing,elev,sampdate,prcp
>> Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-01,0.59
>> Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-02,0.08
>> Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-03,0.1
>> Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-04,0
>> Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-05,0
>> Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-06,0.02
>> Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-07,0.05
>> Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-08,0.1
>> Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-09,0
>> Headworks Portland Water,2370575.38427211,199337.634652112,228,2005-01-10,0.02
>> 
>> There are a bunch of NAs in the data file.
>> 
>> The command to read it produces an error:
>> 
>> rainfall <- read.csv('../data/precipitation/precip_projected.csv', header = T, sep = ',', as.is = T)
>> 
>> Error in `$<-.data.frame`(`*tmp*`, date, value = numeric(0)) :
>> replacement has 0 rows, data has 113569
>> 
>> Is the error generated by finding a date that looks like the number zero
>> or by a prcp value of zero?
>> 
>> BTW, I get the same error if I specify stringsAsFactors = F.
>> 
>> TIA,
>> 
>> Rich
>> 
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> -- 
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

Ecological Forecasting: https://eco.bigelow.org/






	[[alternative HTML version deleted]]




More information about the R-help mailing list