Hello, With the following code get the results array res3<-table(df$v_source,df$v_destine) 1 2 3 4 5 6 7 1 0 10 0 0 0 0 0 2 11 0 0 0 0 0 0 3 0 0 18 15 0 0 0 4 0 0 15 11 0 0 0 5 0 0 0 0 1 0 0 6 0 0 0 0 0 1 0 7 0 0 0 0 0 0 18 my idea was to create a new table of results from res3 but making the sum of the results where the position of the origin and destination were reversed. for example [1,2]= 10 [2,1]=11 New Table Final. points result [1 and 2] 21 thanks -- View this message in context: http://r.789695.n4.nabble.com/Sum-results-in-a-matrix-tp4468936p4468936.html Sent from the R help mailing list archive at Nabble.com.
res3 + t(res3) Michael On Tue, Mar 13, 2012 at 8:15 AM, RMSOPS <ricardosousa2000 at clix.pt> wrote:> Hello, > ? ? With the following code get the results array > res3<-table(df$v_source,df$v_destine) > > ?1 ?2 ?3 ?4 ?5 ?6 ?7 > ?1 ?0 10 ?0 ?0 ?0 ?0 ?0 > ?2 11 ?0 ?0 ?0 ?0 ?0 ?0 > ?3 ?0 ?0 18 15 ?0 ?0 ?0 > ?4 ?0 ?0 15 11 ?0 ?0 ?0 > ?5 ?0 ?0 ?0 ?0 ?1 ?0 ?0 > ?6 ?0 ?0 ?0 ?0 ?0 ?1 ?0 > ?7 ?0 ?0 ?0 ?0 ?0 ?0 18 > > > my idea was to create a new table of results from res3 but making the sum of > the results where the position of the origin and destination were reversed. > > ? ?for example > ? ? ? ? [1,2]= 10 > ? ? ? ? ?[2,1]=11 > > ?New Table Final. > ?points ? ? ? ? result > ?[1 and 2] ? ?21 > > thanks > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Sum-results-in-a-matrix-tp4468936p4468936.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.
thank you It is working, a question in the wake of the array. with the following code I am creating a data frame to store the data without repeating the code is working. The question is the best way to do this process in R tab<-NULL for(i in 1: nrow(res4)) { for(j in i:nrow(res4)) { #print(paste(i,"-",j,"-",res4[i,j])) temp<-data.frame(i,j,res4[i,j]) tab<-rbind(tab,temp) } } tab i j res4.i..j. 1 1 1 0 2 1 2 21 3 1 3 0 4 1 4 0 5 1 5 0 6 1 6 0 7 1 7 0 8 2 2 0 -- View this message in context: http://r.789695.n4.nabble.com/Sum-results-in-a-matrix-tp4468936p4471368.html Sent from the R help mailing list archive at Nabble.com.
On Wed, Mar 14, 2012 at 02:28:22AM -0700, RMSOPS wrote:> thank you > > It is working, a question in the wake of the array. > > with the following code I am creating a data frame to store the data without > repeating the code is working. > The question is the best way to do this process in R > > tab<-NULL > for(i in 1: nrow(res4)) > { > for(j in i:nrow(res4)) > { > #print(paste(i,"-",j,"-",res4[i,j])) > temp<-data.frame(i,j,res4[i,j]) > tab<-rbind(tab,temp) > } > }Hi. Try the following. # create a matrix res4 <- matrix(1:12, nrow=3, ncol=4) d <- dim(res4) ind <- expand.grid(i=1:d[1], j=1:d[2]) cbind(ind, res4=c(res4)) i j res4 1 1 1 1 2 2 1 2 3 3 1 3 4 1 2 4 5 2 2 5 6 3 2 6 7 1 3 7 8 2 3 8 9 3 3 9 10 1 4 10 11 2 4 11 12 3 4 12 Hope this helps. Petr Savicky.