[R] Simple question about error on CSV import

William Dunlap wdunlap at tibco.com
Tue Sep 1 18:24:53 CEST 2009


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of esawdust
> Sent: Tuesday, September 01, 2009 8:53 AM
> To: r-help at r-project.org
> Subject: [R] Simple question about error on CSV import
> 
> 
> 
> I have a substantial CSV to import but can't seem to import even the
> simplest CSV.
> 
> I'm running the latest stable REvolution R on OS X if that is 
> pertinent.
> 
> Here's the contents of a simple test2.csv CSV file:
> 
> #,Status,Project  
> 5842,New,Test
> 
> > snortalerts = read.table( 
> "/Users/lcox/Documents/test2.csv", header=TRUE,
> > sep=",", row.names="#")
> Error in data[[rowvar]] : attempt to select less than one element
> 
> I can't see how it could get any more simple, yet it doesn't 
> work.  I'm
> obviously missing something basic, but based on the error, I 
> can't see what
> it is.

Using '#' for the initial column name presents 2 problems:
   (a) # is the default comment character so that line is
       ignored.  Add the read.table argument comment.char=""
       to take care of that.
   (b) It looks like the column names are converted to legal
       R names before the row.names="name" is processed
       and the "#" is not legal so it becomes something random
       like "X.".  If you turn off this conversion to legal names
       with check.names=FALSE then things will work.  It
       might be better to use the index of the row.names column
       instead of the name, as in row.names=1.

E.g.,

> t<-"#,Status,Project\n5842,New,Test\n"
> read.table(textConnection(t), header=TRUE, sep=",", comment.char="",
check.names=FALSE, row.names="#")
     Status Project
5842    New    Test
> read.table(textConnection(t), header=TRUE, sep=",", comment.char="",
row.names=1)
     Status Project
5842    New    Test

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com 
 
> Landon
> -- 
> View this message in context: 
> http://www.nabble.com/Simple-question-about-error-on-CSV-impor
> t-tp25242899p25242899.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
> 




More information about the R-help mailing list