[R] read.table or read.csv without row index?

JiHO jo.lists at gmail.com
Wed May 5 21:49:51 CEST 2010


> I tried as.matrix but it did not help.

as.matrix() won't work because a matrix requires everything in it to
be of the same type (number, character, logical etc.). You do not have
only numbers in your data.frame, so it will convert everything to
character strings. If you try as.matrix(temp[,-1]) it should work
(assuming you only have characters in the first column, otherwise
remove all non-numeric columns).

But what you really want is to circumvent the fact that, on a
data.frame, mean works column-wise. In fact, when you call mean() on a
data.frame() it calls mean.data.frame(), which code you can see by
typing its name at the prompt:

    > mean.data.frame
    function (x, ...)
    sapply(x, mean, ...)
    <environment: namespace:base>

And indeed, it uses sapply() to apply the function mean to each
column. You could have tried:

    mean.default(temp[2,2:3])

but this returns and error because it needs a numeric vector and does
not automatically convert your data.frame into it. So you need:

    mean.default(as.numeric(temp[2,2:3]))

or more simply

    mean(as.numeric(temp[2,2:3]))

I hope that helped. Sincerely,

JiHO
---
http://maururu.net



More information about the R-help mailing list