Re: [R] data transformation

From: Henrique Dallazuanna <wwwhsd_at_gmail.com>
Date: Fri, 02 May 2008 19:47:32 -0300

Try this:

newx <- with(x, cbind(stack(x, select = grep("spec", names(x))), lat, lon)) newx[newx$values > 0, -1]

On 5/2/08, Christian Hof <chof_at_bio.ku.dk> wrote:
>
> Dear all,
> how can I, with R, transform a presence-absence (0/1) matrix of species
> occurrences into a presence-only table (3 columns) 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), as given in the below example?
> Thanks a lot for your help!
> Christian
>
>
> my dataframe:
>
> site lat lon spec1 spec2 spec3 spec4
> site1 10 11 1 0 1 0
> site2 20 21 1 1 1 0
> site3 30 31 0 1 1 1
>
>
> my desired new dataframe:
>
> species lat lon
> spec1 10 11
> spec1 20 21
> spec2 20 21
> spec2 30 31
> spec3 10 11
> spec3 20 21
> spec3 30 31
> spec4 30 31
>
>
>
> --
> Christian Hof, PhD student
>
> Center for Macroecology & Evolution
> University of Copenhagen
> www.macroecology.ku.dk
> &
> Biodiversity & Global Change Lab
> Museo Nacional de Ciencias Naturales, Madrid
> www.biochange-lab.eu
>
> mobile ES .. +34 697 508 519
> mobile DE .. +49 176 205 189 27
> mail .. chof_at_bio.ku.dk
> mail2 .. vogelwart_at_gmx.de
> blog .. www.vogelwart.de
>
> ______________________________________________
> 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.
>

-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

	[[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.

Received on Fri 02 May 2008 - 22:51:13 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 Fri 02 May 2008 - 23:30:33 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