Hi Elyse, Tom referred to the mefa package, I'd like to draw the attention to the mefa4 package which is more efficient and can handle large data since it uses sparse matrices through the Matrix package. The Melt() function is the inverse operation to xtabs() or its modified version Xtabs() in mefa4. It can take a matrix, sparse matrix, S3 mefa or S4 Mefa object as its argument:
library(mefa4) y <- matrix(sample(0:4, 12, replace=TRUE, prob=c(0.5, rep(0.5/4, 4))), 4, 3) x <- data.frame(x=runif(4), y=runif(4)) colnames(y) <- LETTERS[1:3] rownames(y) <- rownames(x) <- paste("row", 1:4, sep="_") z <- Melt(y) data.frame(z, x[z$rows,]) There is also the (relatively) new multitable package by Steve Walker with similar functionality. Lots of ways to do it -- the problem must bother many of us :) Cheers, Peter Péter Sólymos Alberta Biodiversity Monitoring Institute and Boreal Avian Modelling project Department of Biological Sciences CW 405, Biological Sciences Bldg University of Alberta Edmonton, Alberta, T6G 2E9, Canada Phone: 780.492.8534 Fax: 780.492.7635 email <- paste("solymos", "ualberta.ca", sep = "@") http://www.abmi.ca http://www.borealbirds.ca http://sites.google.com/site/psolymos On Fri, Apr 20, 2012 at 1:06 PM, <tom_phili...@nps.gov> wrote: > Elyse-- > > For the first step of converting your community matrix to triplets of > {site, species, presence}, you can use melt() from the flexible & powerful > reshape package as David suggested, or dematrify() from Dave Roberts' > labdsv package, which was built specifically for this task and has simpler > syntax than melt. The second step is to then merge your location dataframe > with the long-format occurrence data by sitenames. > > Do you need coordinates for only presences, or for presences and absences? > If you need only presences, dematrify() with thresh=0.5 would automatically > drop absences, while melt() would require a second step LongOccurrences <- > LongOccurrences[Freq>0,] > > So, (untested code) for dataframes named CommMatrix and SiteCoords and > sitenames as Site: > LongOccurrences <- dematrify(CommMatrix,thresh=0.5) > Presences <- merge(LongOccurrences,SiteCoords,by="Site",all.x=TRUE) > > If you have more complex needs for handling community and environmental > data, you might consider the mefa package. > > Tom > ------------------------------------------- > Tom Philippi > Quantitative Ecologist > Inventory and Monitoring Program > National Park Service > c/o Cabrillo National Monument > 1800 Cabrillo Memorial Dr > San Diego, CA 92106 > (619) 523-4576 > tom_phili...@nps.gov > http://science.nature.nps.gov/im/monitor > ------------------------------------------- > > > > > David Valentim > Dias > <dvdscripter@gmai To > l.com> "r-sig-ecology@r-project.org" > Sent by: <r-sig-ecology@r-project.org> > r-sig-ecology-bou cc > n...@r-project.or > g Subject > Re: [R-sig-eco] Presence-absence to > Occurrence Dataset? > 04/20/2012 01:32 > PM AST > > > > > > > > > You can check melt() (from reshape2) and reshape() functions. > > > 2012/4/20 Coffey, Elyse D. (UMSL-Student) <edc...@mail.umsl.edu> > >> Hello everyone, >> >> I am working with a large community dataset where column names are > species >> and rows are filled with either 1s or 0s (presence absence of the > species) >> and each row corresponds to a site name. I have long and lati > coordinates >> for each site in a separate datasheet. >> >> I am trying to use a program called Maxent in order model species >> distributions. The program requires a specific occurrence dataset with >> columns in this format: species, Long, Lati. >> >> It is very difficult and time consuming to create such a dataset by hand! >> I was wondering if anyone had any ideas as to how I can use R to > manipulate >> my original community dataset in order to create the occurrence dataset I >> described above? >> >> Any suggestions would be greatly appreciated! >> >> Thanks, >> >> Elyse Coffey >> Biology (B.S.) student >> University of Missouri-St. Louis >> >> >> [[alternative HTML version deleted]] >> >> _______________________________________________ >> R-sig-ecology mailing list >> R-sig-ecology@r-project.org >> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology >> > > > > -- > Currículo: http://lattes.cnpq.br/7541377569511492 > > [[alternative HTML version deleted]] > > _______________________________________________ > R-sig-ecology mailing list > R-sig-ecology@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-ecology > > _______________________________________________ > R-sig-ecology mailing list > R-sig-ecology@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-sig-ecology > _______________________________________________ R-sig-ecology mailing list R-sig-ecology@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-ecology