[R-sig-eco] vegdist producing empty result object

Jari Oksanen jari.oksanen at oulu.fi
Thu Apr 8 08:39:37 CEST 2010


On 7/04/10 21:02 PM, "Chagaris, Dave" <Dave.Chagaris at myfwc.com> wrote:

> I have a dataframe of abundances with 385 rows (samples) and 82 columns
> (species).  I am trying to compute the Bray-Curtis dissimilarity between
> samples.  For some reason, the resulting distance object output by vegdist is
> empty and when I dim the object the result is NULL.  I don't get any error
> messages.  The same dataset works in matlab and  Primer.
> 
> Does anybody know what might be causing this?  The output is below.  Thanks.
> 
>>   dim(guts)
> [1] 385  82
> 
>>   disBray <- vegdist(guts, method="bray")
> 
>>   dim(disBray)
> NULL
> 
>>   is.matrix(disBray)
> [1] FALSE

Dave,

Peter Solymos already told you how to change the vegdist result object (of
class "dist") to a matrix. This is a more general answer. I wonder, did you
ever look at your result before sending a message that it was "empty"? If
you write the name of the result object (disBray) on the screen, you'll see
if it contains something. Or were the lacking dimensions the only hint of
emptiness?

Now some basic R: 'dim' is an attribute of an object, and not all non-empty
objects have that attributes. For instance, a simple vector does not have
dimensions:

> x <- runif(4)
> dim(x)
NULL
> length(x)
[1] 4
> x
[1] 0.9534181 0.4609249 0.0857924 0.3729934

So here 'x' has no dimensions although it has four elements and has a
property of being of length 4. Actually, "dist" objects (like vegdist()
results) are of similar type as the vector 'x' above: They are simple
vectors of numbers with some extra attributes, but no 'dim' attribute:

> d <- vegdist(dune)
> dim(d)
NULL
> length(d)
[1] 190
> str(attributes(d))
List of 7
 $ Size  : int 20
 $ Labels: chr [1:20] "2" "13" "4" "16" ...
 $ Diag  : logi FALSE
 $ Upper : logi FALSE
 $ method: chr "bray"
 $ call  : language vegdist(x = dune)
 $ class : chr "dist"

Functions that handle "dist" objects now how to interpret the single list.
The key attribute here is "Size" which also gives the dimensions of a "dist"
object cast to a square matrix:

> str(attributes(as.matrix(d)))
List of 2
 $ dim     : int [1:2] 20 20
 $ dimnames:List of 2
  ..$ : chr [1:20] "2" "13" "4" "16" ...
  ..$ : chr [1:20] "2" "13" "4" "16" ...

Techically, a "dist" object contains the lower triangle (without the
diagonal) of a symmetric matrix of distances, with attributes which tell how
to interpret the results. This also means that distances or dissimilarities
of "dist" objects must be symmetric, and the distance from an item to itself
must be zero. 

Finally, you can have dimensions in objects which for all practical purposes
are empty:

> m <- matrix(NA, nrow=0, ncol=0)
> dim(m)
[1] 0 0
> m
<0 x 0 matrix>

Looking at a single attributes (like "dim") is not a way of checking
non-emptiness. You have other tools for this, like looking at the object (or
writing a query to a mailing list).

Cheers, Jari Oksanen



More information about the R-sig-ecology mailing list