Hi All, is there any chance of vectorising the two ifelse() statements in the following code: for(i in gp){ new[i,1] = ifelse(srow[i]>0, new[srow[i],zippo[i]], sample(1:100, 1, prob =Y1, rep = T)) new[i,2] = ifelse(drow[i]>0, new[drow[i]>0,zappo[i]], sample(1:100, 1, prob =Y1, rep = T)) } Where I am forced to check if the value of drow and srow are >0 for each line... in practical terms, I am attributing haplotypes to a pedigree, so I have to give the haplotypes to the parents before I give them to the offspring. The vectors *zippo* and *zappo* are the chances of getting one or the other hap from the sire and dam respectively. *gp* is the vectors of non-ancestral animals. *new* is a two col matrix where the haps are stored. Cheers, Federico -- Federico C. F. Calboli Department of Epidemiology and Public Health Imperial College, St Mary's Campus Norfolk Place, London W2 1PG Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193 f.calboli [.a.t] imperial.ac.uk f.calboli [.a.t] gmail.com
The code as you provided it is a bit unusual. In the second assignment, you're using "drow[i]>0" as an index into "new," but ifelse has already found that condition to be true, which means what you wrote is just the same as saying new[1,zappo[i]]. Also, if zappo and zappo are vectors of probabilities, why are they being used as indices into new? Indices are supposed to be integers or T/Fs. It would be nice if you could provide some example data. I'm sure there's a way to vectorize it, but I'm struggling to get my head around the Zippos, zappos and haplos. Kevin -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Federico Calboli Sent: Thursday, July 21, 2005 4:44 PM To: r-help Subject: [R] vectorising ifelse() Hi All, is there any chance of vectorising the two ifelse() statements in the following code: for(i in gp){ new[i,1] = ifelse(srow[i]>0, new[srow[i],zippo[i]], sample(1:100, 1, prob =Y1, rep = T)) new[i,2] = ifelse(drow[i]>0, new[drow[i]>0,zappo[i]], sample(1:100, 1, prob =Y1, rep = T)) } Where I am forced to check if the value of drow and srow are >0 for each line... in practical terms, I am attributing haplotypes to a pedigree, so I have to give the haplotypes to the parents before I give them to the offspring. The vectors *zippo* and *zappo* are the chances of getting one or the other hap from the sire and dam respectively. *gp* is the vectors of non-ancestral animals. *new* is a two col matrix where the haps are stored. Cheers, Federico -- Federico C. F. Calboli Department of Epidemiology and Public Health Imperial College, St Mary's Campus Norfolk Place, London W2 1PG Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193 f.calboli [.a.t] imperial.ac.uk f.calboli [.a.t] gmail.com ______________________________________________ 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
Does either 'zippo' or 'zappo' contain the values 1 or 2 ? If so, then you cannot vectorize this code because you are changing the values in 'new' at every iteration and potentially sampling a value from new[ ,1] or new[ ,2] . If not, then it might be possible to vectorize. Something along the following untested lines pos <- which( srow > 0 ) neg <- which( srow <= 0 ) new[pos ,1] <- new[ cbind(srow[pos] , zippo[pos]) ] new[neg, 1] <- sample( 1:100, length(neg), prob=Y1, replace=TRUE ) and then repeat for filling in new[ ,2]. Am I correct in guessing that your srow and zippo are of the equal length here and thus new is a square matrix. Regards, Adai On Fri, 2005-07-22 at 00:44 +0100, Federico Calboli wrote:> Hi All, > > is there any chance of vectorising the two ifelse() statements in the > following code: > > for(i in gp){ > new[i,1] = ifelse(srow[i]>0, new[srow[i],zippo[i]], sample(1:100, 1, > prob =Y1, rep = T)) > new[i,2] = ifelse(drow[i]>0, new[drow[i]>0,zappo[i]], sample(1:100, > 1, prob =Y1, rep = T)) > } > > Where I am forced to check if the value of drow and srow are >0 for each > line... in practical terms, I am attributing haplotypes to a pedigree, > so I have to give the haplotypes to the parents before I give them to > the offspring. The vectors *zippo* and *zappo* are the chances of > getting one or the other hap from the sire and dam respectively. *gp* is > the vectors of non-ancestral animals. *new* is a two col matrix where > the haps are stored. > > Cheers, > > Federico >
On 22 Jul 2005, at 11:20, Adaikalavan Ramasamy wrote:> Does either 'zippo' or 'zappo' contain the values 1 or 2 ? > > > If so, then you cannot vectorize this code because you are changing > the > values in 'new' at every iteration and potentially sampling a value > from > new[ ,1] or new[ ,2] . >That's exactly my situation, and is exactly what I want to do. After taking out the typo (and bug) "drow[i]>0" the code seems to work fast enough... I'll tinker a bit with it, but it could be good enough as it is. Cheers, Federico Calboli -- Federico C. F. Calboli Department of Epidemiology and Public Health Imperial College, St. Mary's Campus Norfolk Place, London W2 1PG Tel +44 (0)20 75941602 Fax +44 (0)20 75943193 f.calboli [.a.t] imperial.ac.uk f.calboli [.a.t] gmail.com