Billy_Schweiger at nps.gov
2008-Jan-25 21:29 UTC
[R] Function for translation of a list into a matrix as used by ordination?
Hello. Does anyone know of an existing function that takes a list in the form of: Plot1 Species1 Abundance1 Plot1 Species2 Abundance2 Plot2 Species1 Abundance1 Plot2 Species3 Abundance3 . . . PlotN SpeciesN AbundanceN and translates into a matrix in the form of Species1 Species2.... SpeciesN Plot1 Abundance1 Abundance2... AbundanceN Plot2 Abundance1 Abundance2... AbundanceN . . . PlotN AbundanceN AbundanceN... AbundanceN This is a basic operation used in the initial stages of ordination etc of ecological data (vegetation or other species) that is usually stored in a database or flatfile in the list format above. It is similar to a cross tabulation like the function xtabs(), however its useful to be able to control whether a measured abundance for a taxa is included or a boolean (presence/absence) for the taxa in a given plot. Im new to such analyses in R, but before writing it myself hope or suspect that such a function or library is out there (although I dont see it after some quick searching in things like Vegan or BiodivesityR or labdsv). Thanks billy
Christos Hatzis
2008-Jan-25 21:59 UTC
[R] Function for translation of a list into a matrix as used byordination?
Take a look at ?reshape - it is used to convert between 'long' and 'wide' formats of data tables (frames). -Christos> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of > Billy_Schweiger at nps.gov > Sent: Friday, January 25, 2008 4:30 PM > To: r-help at r-project.org > Subject: [R] Function for translation of a list into a matrix > as used byordination? > > > Hello. > > Does anyone know of an existing function that takes a list in > the form of: > > Plot1 Species1 Abundance1 > Plot1 Species2 Abundance2 > Plot2 Species1 Abundance1 > Plot2 Species3 Abundance3 > . > . > . > PlotN SpeciesN AbundanceN > > and translates into a matrix in the form of > > Species1 Species2.... SpeciesN > Plot1 Abundance1 Abundance2... AbundanceN > Plot2 Abundance1 Abundance2... AbundanceN > . > . > > . > PlotN AbundanceN AbundanceN... AbundanceN > > > This is a basic operation used in the initial stages of > ordination etc of ecological data (vegetation or other > species) that is usually stored in a database or flatfile in > the list format above. It is similar to a cross tabulation > like the function xtabs(), however its useful to be able to > control whether a measured abundance for a taxa is included > or a boolean > (presence/absence) for the taxa in a given plot. > > Im new to such analyses in R, but before writing it myself > hope or suspect that such a function or library is out there > (although I dont see it after some quick searching in things > like Vegan or BiodivesityR or labdsv). > > > Thanks > billy > > ______________________________________________ > 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. > >
John Kane
2008-Jan-25 23:20 UTC
[R] Function for translation of a list into a matrix as used by ordination?
I think you can do this fairly easily with the reshape package. Is this basically what you want? x <- "Plot1 Species1 Abundance1 Plot1 Species2 Abundance2 Plot2 Species1 Abundance1 Plot2 Species3 Abundance3 " xx <- read.table(textConnection(x), header=TRUE, as.is=TRUE); xx names(xx) <- c("plot", "species", "abon") ab <- melt(xx, id=c("plot", "species"), measured=c("abon")) cast(ab, plot~ species + variable) --- Billy_Schweiger at nps.gov wrote:> > Hello. > > Does anyone know of an existing function that takes > a list in the form of: > > Plot1 Species1 Abundance1 > Plot1 Species2 Abundance2 > Plot2 Species1 Abundance1 > Plot2 Species3 Abundance3 > . > . > . > PlotN SpeciesN AbundanceN > > and translates into a matrix in the form of > > Species1 Species2.... SpeciesN > Plot1 Abundance1 Abundance2... AbundanceN > Plot2 Abundance1 Abundance2... AbundanceN > . > . > > . > PlotN AbundanceN AbundanceN... AbundanceN > > > This is a basic operation used in the initial stages > of ordination etc of > ecological data (vegetation or other species) that > is usually stored in a > database or flatfile in the list format above. It is > similar to a cross > tabulation like the function xtabs(), however its > useful to be able to > control whether a measured abundance for a taxa is > included or a boolean > (presence/absence) for the taxa in a given plot. > > Im new to such analyses in R, but before writing it > myself hope or suspect > that such a function or library is out there > (although I dont see it after > some quick searching in things like Vegan or > BiodivesityR or labdsv). > > > Thanks > billy > > ______________________________________________ > 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
2008-Jan-26 18:06 UTC
[R] Function for translation of a list into a matrix as used by ordination?
Try this also: noquote(tapply(x$Abundance, list(x$Plot, x$Species), paste)) On 25/01/2008, Billy_Schweiger at nps.gov <Billy_Schweiger at nps.gov> wrote:> > Hello. > > Does anyone know of an existing function that takes a list in the form of: > > Plot1 Species1 Abundance1 > Plot1 Species2 Abundance2 > Plot2 Species1 Abundance1 > Plot2 Species3 Abundance3 > . > . > . > PlotN SpeciesN AbundanceN > > and translates into a matrix in the form of > > Species1 Species2.... SpeciesN > Plot1 Abundance1 Abundance2... AbundanceN > Plot2 Abundance1 Abundance2... AbundanceN > . > . > > . > PlotN AbundanceN AbundanceN... AbundanceN > > > This is a basic operation used in the initial stages of ordination etc of > ecological data (vegetation or other species) that is usually stored in a > database or flatfile in the list format above. It is similar to a cross > tabulation like the function xtabs(), however its useful to be able to > control whether a measured abundance for a taxa is included or a boolean > (presence/absence) for the taxa in a given plot. > > Im new to such analyses in R, but before writing it myself hope or suspect > that such a function or library is out there (although I dont see it after > some quick searching in things like Vegan or BiodivesityR or labdsv). > > > Thanks > billy > > ______________________________________________ > 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
Billy_Schweiger at nps.gov
2008-Jan-28 17:53 UTC
[R] Function for translation of a list into a matrix as used by ordination?
Thanks for all who commented on this question. It turns out that there is a nice set of functions in library BiodiversityR that translate a stack or list input data file to a community matrix: import.from.Excel(file = file.choose(), sheet = "community", sitenames "sites", column = "species", value = "abundance", factor = "", level = "") import.from.Access(file = file.choose(), table = "community", sitenames "sites", column = "species", value = "abundance", factor = "", level = "") I believe the other contributions set to the list worked to varying degrees as well. billy