[R] Specaccum

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Feb 20 14:57:47 CET 2008


On Wed, 2008-02-20 at 13:24 +0100, Alfonso Pérez Rodríguez wrote:
> Hello, I'm trying to obtain a species abundance curve with the
> function  specaccum from vegan package. I've applied this function to
> my data and I've obtained something like this:
> 
> Sites    1.000000 2.000000 3.000000 4.000000  5.0000000  6.0000000
> 7.00  Richness 6.510000 8.290000 9.150000 9.770000 10.3300000
> 10.7600000 sd       1.381662 1.350608 1.200799 1.117853  0.9749903
> 0.9333333  0.90  

That is just the _printed_ representation of the resulting object. In R,
don't believe your eyes - what is printed is not always an accurate
reflection of the complete object!

> 
> But the problem is that I want to export this data to an excel file,
> or a txt file, but I'm not able to do it because I can not asign this
> values to a data.frame or a matrix or something. When I try to
> transform in a data frame the program tell me:
> 
> Error en as.data.frame.default(x[[i]], optional = TRUE,
> stringsAsFactors = stringsAsFactors) : 
>   cannot coerce class "specaccum" into a data.frame
> 
> Then, what can I do? Thank you very much.

I'll start with a reproducible example, from ?specaccum

require("vegan")
data(BCI)
sp1 <- specaccum(BCI)
str(sp1)

The latter call yields:

List of 6
 $ call    : language specaccum(comm = BCI)
 $ method  : chr "exact"
 $ sites   : int [1:50] 1 2 3 4 5 6 7 8 9 10 ...
 $ richness: num [1:50]  90.8 121.6 139.0 150.7 159.2 ...
 $ sd      : num [1:50] 6.93 7.19 7.00 6.64 6.25 ...
 $ perm    : NULL
 - attr(*, "class")= chr "specaccum"

Note the class - "specaccum" - and R has no idea how to convert this to
a data frame or matrix - there is no as.data.frame.specaccum() and this
is because how do you combine 3 vectors of length 50 into a data frame
with other information that is of a different type and length? You
can't.

So, now your problem boils down to extracting the relevant information
from sp1 and writing that out as a data frame. This is very easy. Given
your email I presume you are interested in the 'sites', 'richness' and
'sd' components:

my.res <- with(sp1, data.frame(sites, richness, sd))
head(my.res)

The latter call yielding the first 6 lines of my.res (there are 50 in
total):

  sites richness       sd
1     1  90.7800 6.934811
2     2 121.6098 7.193362
3     3 139.0459 7.001993
4     4 150.7116 6.635769
5     5 159.2363 6.248117
6     6 165.8306 5.894738

Now just write out my.res using write.table()

write.table(my.res, file = "my.results.txt")

Which works for me.

Is that what you wanted?

HTH

G

> 
> 
> 
> 
> Alfonso Prez Rodrguez
> Instituto de Investigaciones Marinas
> C/ Eduardo Cabello n 6 
> C.P. 36208 Vigo (Espaa)
> Tlf.- 986231930 Extensin 241
> e-mail: fonsilei at iim.csic.es
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list