Dear All, I have a problem in making a matrix from a data. I did a summary data from a big data frame using (ddply), the resulting table is like that: Site.No Plant.Name Cover.Percentage 1 XXX 15 1 YYY 20 2 XXX 18 2 YYY 21 3 XXX 14 3 YYY 26 I need to convert it to the following: XXX YYY 1 15 20 2 18 21 3 14 26 I tried for the whole day yesterday but I can't solve it. Could you please help me. Thanks in advance for all of you, Hamada -- View this message in context: http://r.789695.n4.nabble.com/Problem-with-a-Matrix-tp4455954p4455954.html Sent from the R help mailing list archive at Nabble.com.
Hi Hamada, Could the following be helpful? "dtf" is your data.frame? nr <- nrow(dtf) new.dtf <- data.frame(Site.No=seq(1,nr/2)) new.dtf$XXX <- dtf[seq(1,nr,2),3] new.dtf$YYY <- dtf[seq(2,nr,2),3] or new.mat <- numeric() new.mat <- cbind(new.mat,dtf[seq(1,nr,2),3]) new.mat <- cbind(new.mat,dtf[seq(2,nr,2),3]) Regards, Pascal ----- Mail original ----- De?: Hamada Elsayed Ali <medo_botany at hotmail.com> ??: r-help at r-project.org Cc?: Envoy? le : Jeudi 8 mars 2012 18h20 Objet?: [R] Problem with a Matrix Dear All, I have a problem in making a matrix from a data. I did a summary data from a big data frame using (ddply), the resulting table is like that: Site.No? ? Plant.Name? ? Cover.Percentage 1? ? ? ? ? ? ? ? XXX? ? ? ? ? ? ? ? 15 1? ? ? ? ? ? ? ? YYY? ? ? ? ? ? ? ? 20 2? ? ? ? ? ? ? ? XXX? ? ? ? ? ? ? ? 18 2? ? ? ? ? ? ? ? YYY? ? ? ? ? ? ? ? 21 3? ? ? ? ? ? ? ? XXX? ? ? ? ? ? ? ? 14 3? ? ? ? ? ? ? ? YYY? ? ? ? ? ? ? ? 26 I need to convert it to the following: ? ? ? ? XXX? ? ? YYY 1? ? ? 15? ? ? ? 20 2? ? ? 18? ? ? ? 21 3? ? ? 14? ? ? ? 26 I tried for the whole day yesterday but I can't solve it. Could you please help me. Thanks in advance for all of you, Hamada -- View this message in context: http://r.789695.n4.nabble.com/Problem-with-a-Matrix-tp4455954p4455954.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Hi> > Dear All, > > I have a problem in making a matrix from a data. I did a summary datafrom a> big data frame using (ddply), the resulting table is like that: > > Site.No Plant.Name Cover.Percentage > 1 XXX 15 > 1 YYY 20 > 2 XXX 18 > 2 YYY 21 > 3 XXX 14 > 3 YYY 26 > > I need to convert it to the following: > > XXX YYY > 1 15 20 > 2 18 21 > 3 14 26seems to be a work for reshape or cast or maybe xtabs test <-read.table("clipboard", header=T) test Site.No Plant.Name Cover.Percentage 1 1 XXX 15 2 1 YYY 20 3 2 XXX 18 4 2 YYY 21 5 3 XXX 14 6 3 YYY 26 names(test)<-c("site", "plant", "perc")> xtabs(perc~site+plant, data=test)plant site XXX YYY 1 15 20 2 18 21 3 14 26 Regards Petr> > I tried for the whole day yesterday but I can't solve it. Could youplease> help me. > > Thanks in advance for all of you, > Hamada > > -- > View this message in context:http://r.789695.n4.nabble.com/Problem-with-> a-Matrix-tp4455954p4455954.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Hi Petr, Finally, I got the solution like you explained and I find another one like the following: Cover <- tapply(cover[,3], cover[, c(1,2)], c) It is working great. Thanks for your help, Hamada -- View this message in context: http://r.789695.n4.nabble.com/Problem-with-a-Matrix-tp4455954p4456041.html Sent from the R help mailing list archive at Nabble.com.
I think this is what you want. library(reshape2) dcast( xx, Site.No ~ Plant.Name) John Kane Kingston ON Canada> -----Original Message----- > From: medo_botany at hotmail.com > Sent: Thu, 8 Mar 2012 01:20:42 -0800 (PST) > To: r-help at r-project.org > Subject: [R] Problem with a Matrix > > Dear All, > > I have a problem in making a matrix from a data. I did a summary data > from a > big data frame using (ddply), the resulting table is like that: > > Site.No Plant.Name Cover.Percentage > 1 XXX 15 > 1 YYY 20 > 2 XXX 18 > 2 YYY 21 > 3 XXX 14 > 3 YYY 26 > > I need to convert it to the following: > > XXX YYY > 1 15 20 > 2 18 21 > 3 14 26 > > I tried for the whole day yesterday but I can't solve it. Could you > please > help me. > > Thanks in advance for all of you, > Hamada > > -- > View this message in context: > http://r.789695.n4.nabble.com/Problem-with-a-Matrix-tp4455954p4455954.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.____________________________________________________________ Send your photos by email in seconds... TRY FREE IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if3 Works in all emails, instant messengers, blogs, forums and social networks.