[R] strange behavior of my function

Kyeongmi Cheon katie.cheon at gmail.com
Tue Apr 1 17:54:52 CEST 2008


Hello,
I wrote a program to convert normal distribution  to trinomial
distribution (three levels=0,1,2). But the first function I wrote
(convert.to.trinom1) converts everything to 2. When I changed it
slightly (convert.to.trinom2), it works correctly (ie. converts to
0,1, or 2). I cannot figure out why it happens. I even restarted R and
tried again and the same thing happened over and over. I use R 2.6.0
in Windows Vista. Can anyone help me with this? Thank you.
Kyeongmi



norm1=rnorm(20)
norm1

z1=-1.2212272
z2=-0.6744898
convert.to.trinom1=function(data, z1, z2){
    data[data<z1]=0
    data[z1<=data & data<z2]=1
    data[data>=z2]=2
    data
}
trinom1=convert.to.trinom1(norm1,z1, z2)
trinom1

convert.to.trinom2=function(data, z1, z2){
    data[data>=z2]=2
    data[data<z1]=0
    data[z1<=data & data<z2]=1
    data
}
trinom2=convert.to.trinom2(norm1,z1, z2)
trinom2



More information about the R-help mailing list