[R] Ifelse statement

fre fre_stamlid at hotmail.com
Mon Jul 11 21:39:48 CEST 2011


Hello everyone,

I have a (small) issue. I already googled a lot, so I decided to use ifelse
instead of if (){} else{}

All the elements seem to work seperately, but combined in the ifelse
statement, it doesn't seem to work.

#The price function is a function which is normally distributed with only
positive answers
price<-function() {abs(rnorm(1,10,25))}

#Before I use pieceprice in the ifelse, I need it to be defined
pieceprice<-cbind()

#Now I define a function with an ifelse statement. So if the binomial
deviation returns one, I add a 'new' price() to the pieceprice vector else I
repeat the last element of the pieceprice vector and add it to pieceprice.
pricechange<-function()
{ifelse(rbinom(1,1,2/3)==1,
pieceprice<-cbind(pieceprice,price()),
pieceprice<-cbind(pieceprice,pieceprice[1,length(pieceprice)]))}

#But now if I try this (Even with rbinom(1,1,1), pieceprice remains NULL
pricechange()
pieceprice


> pricechange()
[1] 79.20426
> pieceprice
NULL


I hope someone has a clue what's wrong.

Thanks a lot for your help!


--
View this message in context: http://r.789695.n4.nabble.com/Ifelse-statement-tp3660627p3660627.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list