[R] data format of x in x <- sqlQuery()

Jason Turner jasont at indigoindustrial.co.nz
Wed Jun 5 00:59:48 CEST 2002

On Tue, Jun 04, 2002 at 04:11:06PM -0500, Sukhaswami Malladi wrote:
> When I read data into an R variable x using RODBC command:
> x <- sqlQuery(channel,"select a, b from tabl",as="data frame")   
> and view x, I get 3 columns the first one being the index of
> the rows in x. 
> What options do I use in sqlQuery so as not have the index in x ? 
> How do I get rid of the index otherwise.

You never got an index column.  You got a two column data frame.
You can verify this by dim(x) or names(x).  Please do.

When you "view" x by typing x at the R prompt, it just calls
print.data.frame(x), which by default shows the row names.
If your data frame doesn't have row names, it makes them up,
as row numbers.  The command rownames(x) will show what R was

Don't worry about them.

> For some functions like princomp (and boxplot) for example:
> > princomp(x)
> Error in cov.wt(z) : x must contain finite values only

You're sending princomp bad data.  Have you checked for NA values,
Inf, -Inf, etc?



or screen your data.

Possibly, the ODBC driver is converting all your numeric values to 
text; check that too.  help(numeric)  might cast some light.

> There is no error for USArrests example data which 
> does not have index.

Actually, it does.  Remember:  this thing you're calling an index is
just the row names.  The USArrests data frame has rownames, which is 
the first column you see when you type USArrests.  Note that there is 
no "State" column in the USArrests data set.  names(USArrests) and 
rownames(USArrests) will confirm this.

> > boxplot(x)
> Error in x[floor(d)] + x[ceiling(d)] : non-numeric argument to binary
> operator

Again, boxplot(!is.na(x)).  Or check for text/numeric conversion.
boxplot(!is.na(as.numeric(x)) might help.


1) Clean up your data.
2) Next time, please check further before speculating what the cause 
might be.

You do get points for including a description of how the data was
gathered, and the error message, though.  If you'd just asked
about row indices, we'd have been unable to help you.


Indigo Industrial Controls Ltd.
jasont at indigoindustrial.co.nz
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list