[R] Error: no 'dimnames' attribute for array
R. Michael Weylandt
michael.weylandt at gmail.com
Thu Mar 7 12:31:59 CET 2013
On Thu, Mar 7, 2013 at 11:19 AM, eliza botto <eliza_botto at hotmail.com> wrote:
> Thankyou very much M. Weylandt. i was actually more interested in knowing
> about the error.
Let's talk you through it then:
As you said before you have
b1 <- c(1L, 2L, 6L, 7L, 12L, 16L, 17L, 20L, 21L, 23L, 25L, 34L, 46L,
48L, 58L, 64L, 65L, 68L, 82L, 97L, 98L, 101L, 113L, 115L)
in your session. And from there you create:
s<-noquote(paste (b1, collapse=","))
So let's take that apart: you take a set of integers (and integer
vector) and collapse it using the paste() function. This returns a
character string. You pass the resulting character string to noquote()
which is a relatively obscure function. However, it's not a hard one:
it basically attaches the "noquote" class to the object which allows
it to act in almost entirely the same way, but to be printed without
quotes. Note the difference in
print(paste(b1, collapse = ", "))
and
print(noquote(paste(b1, collapse = ", ")))
So anyways, now you've got s and you use it to subscript a matrix.
Here R makes a decision that surprises you: it tries to interpret s as
a character vector! (because it is one) So what does it mean when you
subset by a character vector instead of a numeric index: well, to pull
out the apropriate row or column by _name_ instead of by position. To
do this requires knowing the names: so R looks for the relevant names,
which are normally stored in a property called "dimnames" (dimension
names).
Your matrix, however, doesn't seem to have any dimnames, so R throws
an error saying (in effect) "you want me to get the rows by this name
and I am totally willing to do so, but there's this tiny little issue:
there aren't any names..."
Now this could have turned out differently: your matrix could have had
some names, but none which matched s. Then you'd simply have
successful subsetting with no result, not unlike computing b1[0]. A
much more subtle error. So you should actually consider yourself lucky
you got the error at this stage instead of having to hunt it down in a
much more subtle form later.
So how did I know all of this? Aside from some general familiarity
with R, I used the str() function which lets me take a look at what
"sort" of thing things in R are. (Say that three times fast!) When I
applied it to str(s) I found that s was a character vector of class
noquote -- and that's what made it all clear that your numbers weren't
really being interpreted as "numbers" because, well, they really
weren't numbers from R's point of view.
Again, and just to make that last point clear: they weren't numbers
because there's a fundamental difference between "3" and 3 in
computers. One is the integral successor of 2, the other is a way that
humans happen to denote it at certain points in history in certain
computational contexts. Some languages like Perl let you play a bit
fast and loose with this distinction, but I don't think anyone would
argue that R needs to be more forgiving in its type system.
Cheers,
MW
More information about the R-help
mailing list