[R] Cross-tabulation of data from database

Morten Sickel
Tue, 26 Feb 2002 22:14:37 -0000

I am quite new to R, so please bear over with me if I have problems with the
R terminology.

I want to (try to) use R for some analyses within vegetation ecology, using
the vegan package. I have my data in a postgresql database, and I manage to
get them into R as a dataframe with columns for respectively: Name of the
analysed m2, Name of the species, coverage of species in the square in %. I
want to have the dataset transformed into a table similiar to the example
tables in vegan, i.e, one column for each species, one row for each square.

What I have done so far is to establish a connection to postgres using Rdbi,
fetching the data :
> dca.raw<-dbGetResult(dcaq)
> dca.raw
                                                nornavn dekningsgrad  ruteid
1    Fjellmarik?pe                                                 1 LIASB01
2    Gulaks                                                        3 LIASB01
3    Harerug                                                       1 LIASB01
4    Brearve                                                       1 LIASB01
5    Sveve sp.                                                     5 LIASB01
6    Finnskjegg                                                   50 LIASB01
7    Setergr?urt                                                   1 LIASB01
8    Engrapp-gruppa                                                5 LIASB01
9    Legeveronika                                                  1 LIASB01
etc.
(Norwegian species names, nornavn = species, dekningsgrad= coverage
ruteid=name of square)
then I converted it to a data frame:
> dca.frame <- data.frame(dca.raw)
and
> dca.frame
gives an output that seems reasonable.
so, I thought I could use xtabs and tried:
> xtabs(dekningsgrad ~ ruteid+nornavn, data=dca.frame)
which, as far as I can see, is quite analogous to the inner part of the
example given:
> ftable(xtabs(breaks ~ wool + tension + replicate, data = warpbreaks))

But I end up with:
Error in Summary.factor(..., na.rm = na.rm) :
        "sum" not meaningful for factors

And then,  as a still quite clueless R-user, I am stranded. I have been
searching in the docs and mailinglist, but have not came across much
information on xtabs and its prerequisites, I have probably stumbled into
some quite basic R things, so any help would be greatly appreciated.

Best regards,

--
Morten Sickel
GIS Consultant
Dr?bak, Norway
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._