[R] strange behavior of my function

From: Kyeongmi Cheon <katie.cheon_at_gmail.com>
Date: Tue, 01 Apr 2008 10:54:52 -0500


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

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. Received on Tue 01 Apr 2008 - 16:45:09 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 01 Apr 2008 - 17:30:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive