Simple question about error on CSV import

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Simple question about error on CSV import

esawdust
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.   Any quick help or tips on import and common errors for importing data?

Landon
Reply | Threaded
Open this post in threaded view
|

Re: Simple question about error on CSV import

esawdust
esawdust wrote
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

Landon
Figured out the answer, though it wasn't obvious (to me anyway).  The symbol "#" used as the first column label was the problem.   I changed that to be "id" and changed the read.table to be:

snortalerts <- read.table( "/Users/lcox/Documents/test2.csv", header=TRUE, sep=",", row.names="id")

and it worked fine.  
Reply | Threaded
Open this post in threaded view
|

Re: Simple question about error on CSV import

William Dunlap
In reply to this post by esawdust

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of esawdust
> Sent: Tuesday, September 01, 2009 8:53 AM
> To: [hidden email]
> 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Simple question about error on CSV import

lwaldron
In reply to this post by esawdust
 By default for read.table,

comment.char = "#"

so the first line was being treated as a comment line, and when you
specified row.names="#", read.table couldn't find that column.

On Tue, Sep 1, 2009 at 12:07 PM, esawdust <[hidden email]> wrote:

>
>
> esawdust wrote:
> >
> > 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
> >
> > Landon
> >
>
> Figured out the answer, though it wasn't obvious (to me anyway).  The
> symbol
> "#" used as the first column label was the problem.   I changed that to be
> "id" and changed the read.table to be:
>
> snortalerts <- read.table( "/Users/lcox/Documents/test2.csv", header=TRUE,
> sep=",", row.names="id")
>
> and it worked fine.
> --
> View this message in context:
> http://www.nabble.com/Simple-question-about-error-on-CSV-import-tp25242899p25243159.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>



--
Levi Waldron
post-doctoral fellow
Jurisica Lab, Ontario Cancer Institute
Division of Signaling Biology
TMDT 9-304D
101 College Street
Toronto, Ontario M5G 1L7
(416)581-7453

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Simple question about error on CSV import

esawdust
In reply to this post by esawdust
thank you all for the quick responses and helpful explanations.  I'm just getting started with R, so I'll get tripped up by some of this until I get in the groove.

Thanks again,

Landon