[R] Still avoiding loops

Liaw, Andy andy_liaw at merck.com
Wed Jan 26 15:24:44 CET 2005


See if this does what you want:

> m <- matrix(round(runif(24)), 4, 6)  # simulate some data
> m
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    0    1    0    1    0    1
[2,]    0    1    1    1    0    0
[3,]    1    1    1    0    0    0
[4,]    1    0    0    0    1    0
> library(gtools)  # Install the gregmisc package if you don't have it.
> idx <- combinations(nrow(m), 2)
> res <- m[idx[,1],] + m[idx[,2]]
> rownames(res) <- paste(idx[,1], idx[,2], sep="+")
> res
    [,1] [,2] [,3] [,4] [,5] [,6]
1+2    0    1    0    1    0    1
1+3    1    2    1    2    1    2
1+4    1    2    1    2    1    2
2+3    1    2    2    2    1    1
2+4    1    2    2    2    1    1
3+4    2    2    2    1    1    1

Andy
 

> From: Jacques VESLOT
> 
> It is part of a function to determine Dice's index in the 
> framewok of AFLP
> analysis.
> 
> X is a binary matrix which value for each strain (lines) and 
> each base pair
> (columns) is 1 where there is a peak and 0 where there is no peak as
> biologists explained to me.
> 
> The first step is to compare each strain with one another by 
> counting the
> number of 0, 1 and 2, respectively where there is no peak, 
> one peak or 2
> peaks for each base pair.
> 
> In that respect, I want to add together each pair of X's lines.
> 
> For the moment, there is a double loop calculating, at each 
> step, the sum of
> two lines as a vector Y and counting the number of 0, 1 and 2 
> in it for
> inclusion in other operations.
> 
> I read your posting...
> 
> Thanks for helping,
> 
> Jacques VESLOT
> 
> 
> -----Message d'origine-----
> De : Adaikalavan Ramasamy [mailto:ramasamy at cancer.org.uk]
> Envoyé : mercredi 26 janvier 2005 15:57
> À : jacques.veslot at cirad.fr
> Cc : R-help
> Objet : Re: [R] Still avoiding loops
> 
> 
> Please give a simple example of the input data and output that you
> desire. It is difficult to understand from you partial codes what you
> mean. For example what is Y ?
> 
> Are you trying to find add values from pairs of rows ? If so, 
> please see
> my posting "pairwise difference operator" where I wanted to find the
> differences between pairs of columns.
> http://tolstoy.newcastle.edu.au/R/help/04/07/1633.html
> 
> Otherwise, please send a sample input and output. Thank you.
> 
> Regards, Adai
> 
> 
> On Wed, 2005-01-26 at 11:40 +0400, Jacques VESLOT wrote:
> > Dear all,
> >
> > I have a matrix X with 47 lines and say 500 columns - 
> values are in {0,1}.
> > I'd like to compare lines.
> >
> > For that, I first did:
> >
> > for (i in 1:(dim(X)[1]-1))
> > for (j in (i+1):dim(X)[1]) {
> > 	Y <- X[i,]+Y[j,]
> > 	etc.
> >
> > but, since it takes a long time, I would prefer avoding loops;
> > for that, my first idea was to add this matrix:
> >
> > X1=X[,rep(1:46,46:1)]
> >
> > to this one:
> >
> > res=NULL
> > for (i in (2:47)) res=c(res,i:47)
> >
> > X2=X[,res]
> >
> > (Is it a nice alternative way ?)
> > Is there a way to create the second matrix X2 without a 
> loop, such as for
> X1
> > ?
> >
> > Thanks in advance,
> >
> > Jacques VESLOT
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> >
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list