[R] vectorising ifelse()

Kevin Bartz bartzk at yahoo-inc.com
Fri Jul 22 03:14:03 CEST 2005

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

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.


-----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.



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
PLEASE do read the posting guide!

More information about the R-help mailing list