Dear R-help members I would be grateful if anyone could help me with the following problem: I would like to combine two matrices (Schmitt_15 and Schmitt_16, they are attached) which have a species presence/absence x sampling plot structure. The aim would be to have in the end only one matrix which shows all existing species and their presence/absence on all the different plots. To do this I used the "merge" function in R. The problem is that my matrix in the end shows only 12 species (but there are in total about 100!). I don't know why. I used the following commands: Schmitt_15 Schmitt_16 output<-merge(Schmitt_15,Schmitt_16,by="species") write.table(ab,file="output.txt",sep=",") Can anyone help me? Thank you very much! Michael -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: Schmitt_15.txt URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130314/9ac19d53/attachment.txt> -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: Schmitt_16.txt URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130314/9ac19d53/attachment-0001.txt> -------------- next part --------------
Below , in line John Kane Kingston ON Canada> -----Original Message----- > From: michael.eisenring at gmx.ch > Sent: Thu, 14 Mar 2013 11:51:49 +0100 > To: r-help at r-project.org > Subject: [R] merge function to combine two tables > > Dear R-help members > > I would be grateful if anyone could help me with the following problem: > > I would like to combine two matrices (Schmitt_15 and Schmitt_16, they are > attached) which have a species presence/absence x sampling plot > structure. The aim would be to have in the end only one matrix which > shows all existing species and their presence/absence on all the > different plots. > To do this I used the "merge" function in R. > The problem is that my matrix in the end shows only 12 species (but there > are in total about 100!). I don't know why. > > I used the following commands: > > > Schmitt_15 > Schmitt_16 > output<-merge(Schmitt_15,Schmitt_16,by="species")# you seem to be only picking out the common species in the two data.frames ncol(output) length(unique(output$species)) Schmitt_15$species %in% Schmitt_16$species # This may do what you want. It means that you are taking every speices name found in either file. Is that what you want" newdat <- merge(Schmitt_15,Schmitt_16, by="species", all = TRUE) This gives me a merged file with # You seem to have missed a step here since there is no ab object in your code.> write.table(ab,file="output.txt",sep=",") > > Can anyone help me? > Thank you very much! > Michael > > > > ______________________________________________ > 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.____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
Take a look at your data. When I loaded what you attached, there were only 9 species that were in common across the two files:> dim(s16)[1] 226 83> dim(s15)[1] 96 41> sum(s15$species %in% s16$species)[1] 10> sum(s16$species %in% s15$species)[1] 10> length(intersect(s16$species, s15$species))[1] 9> length(unique(s16$species))[1] 173> length(unique(s15$species))[1] 90> x <- merge(s16, s15, by = 'species') > dim(x)[1] 12 123>so it is not surprising you got the result that you did. On Thu, Mar 14, 2013 at 6:51 AM, Michael Eisenring <michael.eisenring@gmx.ch> wrote:> Dear R-help members > > I would be grateful if anyone could help me with the following problem: > > I would like to combine two matrices (Schmitt_15 and Schmitt_16, they are > attached) which have a species presence/absence x sampling plot structure. > The aim would be to have in the end only one matrix which shows all > existing species and their presence/absence on all the different plots. > To do this I used the "merge" function in R. > The problem is that my matrix in the end shows only 12 species (but there > are in total about 100!). I don't know why. > > I used the following commands: > > > Schmitt_15 > Schmitt_16 > output<-merge(Schmitt_15,Schmitt_16,by="species") > write.table(ab,file="output.txt",sep=",") > > Can anyone help me? > Thank you very much! > Michael > > > > > ______________________________________________ > R-help@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. > >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. [[alternative HTML version deleted]]