It's tempting to think of data.frames as glorified matrices with better
dimension names (i.e. names() and row.names() rather than
dimnames()[] and dimnames()[] respectively) and which we
can access by field name without knowing which column it is. Actually,
data.frames are lists where every list item is required to be
exactly the same length.
The distinction becomes important when we want to perform some operations on the
data frame, especially mathematical operations. For example, if our data on
taxon abudance are in a data.frame called taxon and we want the mean
abundance of all species in our plots we cannot do
and get an answer. What we will get is
In mean.default(taxon) :
argument is not numeric or logical: returning NA
because data.frames are not numerical, even if all the data in the data.frame IS
numerical. Instead we have to do
So, you have two options.
- convert your data.frame to a matrix permanently, and live with the more
syntactically challenging dimnames()[] or
- get used to surrounding your data.frame name with as.matrix if you
want to do math on the whole data.frame.
You won't run into ths problem too often, because usually you're oerating on a
single column (field), but here's a common example.
Error in table(bryceveg) : attempt to make a table with >=
0 0.2 0.5 1 2 3 4 5
24851 3 1818 202 104 36 18 8