Mäkinen Jussi
2005-May-31 14:15 UTC
[R] A suggestion to improve ifelse behaviour with vector yes/no arguments
Dear All, I luckily found the following feature (or problem) when tried to apply ifelse-function to an ordered data.> test <- c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE) > ifelse(test, 0, 1:4)[1] 0 0 0 4 1 2 3>It roots into the ifelse-syntax: ans[!test & !nas] <- rep(no, length.out = length(ans))[!test & !nas] Would it be possible to disable this feature in the next R-version? For instance change the code to be: ans[!test & !nas] <- rep(no, length.out = length(ans[!test & !nas])) which seems to solve the problem. Best regards, Jussi M??kinen Jussi M??kinen Analyst State Treasury, Finland www.statetreasury.fi mailto:jussi.makinen at valtiokonttori.fi
Dimitris Rizopoulos
2005-May-31 14:22 UTC
[R] A suggestion to improve ifelse behaviour with vector yes/noarguments
there is nothing problematic there! According to the help-page of ifelse(), in the Details section you get: "If yes or no are too short, their elements are recycled." ^^^^^^^^ and this is what you get in your example. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "M??kinen Jussi" <Jussi.Makinen at valtiokonttori.fi> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, May 31, 2005 4:15 PM Subject: [R] A suggestion to improve ifelse behaviour with vector yes/noarguments> Dear All, > > I luckily found the following feature (or problem) when tried to > apply ifelse-function to an ordered data. > >> test <- c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE) >> ifelse(test, 0, 1:4) > [1] 0 0 0 4 1 2 3 >> > > It roots into the ifelse-syntax: > > ans[!test & !nas] <- rep(no, length.out = length(ans))[!test & !nas] > > Would it be possible to disable this feature in the next R-version? > For instance change the code to be: > > ans[!test & !nas] <- rep(no, length.out = length(ans[!test & !nas])) > > which seems to solve the problem. > > Best regards, > > Jussi M??kinen > > Jussi M??kinen > Analyst > State Treasury, Finland > www.statetreasury.fi > mailto:jussi.makinen at valtiokonttori.fi > > ______________________________________________ > 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 >
Duncan Murdoch
2005-May-31 15:16 UTC
[R] A suggestion to improve ifelse behaviour with vector yes/no arguments
M??kinen Jussi wrote:> Dear All, > > I luckily found the following feature (or problem) when tried to apply ifelse-function to an ordered data. > > >>test <- c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE) >>ifelse(test, 0, 1:4) > > [1] 0 0 0 4 1 2 3 > > > It roots into the ifelse-syntax: > > ans[!test & !nas] <- rep(no, length.out = length(ans))[!test & !nas] > > Would it be possible to disable this feature in the next R-version? For instance change the code to be: > > ans[!test & !nas] <- rep(no, length.out = length(ans[!test & !nas])) > > which seems to solve the problem.As Dimitris said, this is just recycling. I think getting rid of recycling on vectors with length greater than 1 would have been a good decision in S about 15 years ago, but it's too late now. Duncan Murdoch
Maybe Matching Threads
- A logit question?
- its plot with pch-argument
- A suggestion to improve ifelse behaviour with vector yes/noarguments
- Add-on bug? Win fracdiff failed from http://www.stat.unipg.it/stat/statlib/R/CRAN/ (PR#2505)
- Add-on bug? Win fracdiff failed from http://www.stat.unipg.it/stat/statlib/R/CRAN/ (PR#2504)