Re: [R] data transformation

From: Marc Schwartz <marc_schwartz_at_comcast.net>
Date: Tue, 22 Jul 2008 12:45:53 -0500

on 07/22/2008 11:24 AM Christian Hof wrote:
> Dear all,
>
> how can I, with R, transform a presence-only table (with the names of
> the species (1st column), the lat information of the sites (2nd column)
> and the lon information of the sites (3rd column)) into a
> presence-absence (0/1) matrix of species occurrences across sites, as
> given in the below example?
>
> Thanks a lot for your help!
> Christian
>
>
>
> My initial table:
>
> species lat lon
> sp1 10 10
> sp1 10 30
> sp1 20 10
> sp1 20 20
> sp1 20 30
> sp2 10 30
> sp2 20 30
> sp2 30 30
>
>
> My desired matrix:
>
> lat lon sp1 sp2
> 10 10 1 0
> 10 20 0 0
> 10 30 1 1
> 20 10 1 0
> 20 20 1 0
> 20 30 1 1
> 30 10 0 0
> 30 20 0 0
> 30 30 0 1

One approach would be to use ftable(). Presuming that your source data is in a data frame called 'DF':

 > ftable(species ~ lat + lon, data = DF)

         species sp1 sp2
lat lon

10  10            1   0
     20            0   0
     30            1   1
20  10            1   0
     20            1   0
     30            1   1
30  10            0   0
     20            0   0
     30            0   1



See ?ftable and/or ?ftable.formula

HTH, Marc Schwartz



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. Received on Tue 22 Jul 2008 - 18:23:28 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 22 Jul 2008 - 18:32:00 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive