Re: [R] ifelse question (I'm not sure why this is working)...
> remainderFunction<-function(x,d) > { >ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL)) > } > remainderFunction(x=c(23:47),d=3) The above call returns c(2, 5, 8, 11, 14, 17, 20, 23), the value of (23:47)%%3. Note that remainderFunction(integer(0), 3) returns logical(0). The return() calls in the call to ifelse cause the problem. The one used for the second argument to ifelse causes remainderFunction to return, abandoning the evaluation of ifself, as soon as ifelse evaluates its second argument. I think that using return this way is bad practice, but have seen it in code like tryCatch(return(something), error=function(e)"Error in something") which is common in support code for RStudio. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of Jonathan Greenberg > Sent: Tuesday, September 10, 2013 12:40 PM > To: r-help > Subject: [R] ifelse question (I'm not sure why this is working)... > > R-helpers: > > One of my intrepid students came up with a solution to a problem where > they need to write a function that takes a vector x and a "scalar" d, > and return the indices of the vector x where x %% d is equal to 0 (x > is evenly divisible by d). I thought I had a good handle on the > potential solutions, but one of my students sent me a function that > WORKS, but for the life of me I can't figure out WHY. Here is the > solution: > > remainderFunction<-function(x,d) > { >ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL)) > } > remainderFunction(x=c(23:47),d=3) > > I've never seen an ifelse statement used that way, and I was fully > expecting that to NOT work, or to place the output of which(x%%d==0) > in each location where the statement x%%d==0 was true. > > Any ideas on deconstructing this? > > --j > > -- > Jonathan A. Greenberg, PhD > Assistant Professor > Global Environmental Analysis and Remote Sensing (GEARS) Laboratory > Department of Geography and Geographic Information Science > University of Illinois at Urbana-Champaign > 607 South Mathews Avenue, MC 150 > Urbana, IL 61801 > Phone: 217-300-1924 > http://www.geog.illinois.edu/~jgrn/ > AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 > > __ > R-help@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. __ R-help@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.
[R] ifelse question (I'm not sure why this is working)...
R-helpers: One of my intrepid students came up with a solution to a problem where they need to write a function that takes a vector x and a "scalar" d, and return the indices of the vector x where x %% d is equal to 0 (x is evenly divisible by d). I thought I had a good handle on the potential solutions, but one of my students sent me a function that WORKS, but for the life of me I can't figure out WHY. Here is the solution: remainderFunction<-function(x,d) { ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL)) } remainderFunction(x=c(23:47),d=3) I've never seen an ifelse statement used that way, and I was fully expecting that to NOT work, or to place the output of which(x%%d==0) in each location where the statement x%%d==0 was true. Any ideas on deconstructing this? --j -- Jonathan A. Greenberg, PhD Assistant Professor Global Environmental Analysis and Remote Sensing (GEARS) Laboratory Department of Geography and Geographic Information Science University of Illinois at Urbana-Champaign 607 South Mathews Avenue, MC 150 Urbana, IL 61801 Phone: 217-300-1924 http://www.geog.illinois.edu/~jgrn/ AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 __ R-help@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.
Re: [R] ifelse question (I'm not sure why this is working)...
On 09/11/2013 05:40 AM, Jonathan Greenberg wrote: R-helpers: One of my intrepid students came up with a solution to a problem where they need to write a function that takes a vector x and a "scalar" d, and return the indices of the vector x where x %% d is equal to 0 (x is evenly divisible by d). I thought I had a good handle on the potential solutions, but one of my students sent me a function that WORKS, but for the life of me I can't figure out WHY. Here is the solution: remainderFunction<-function(x,d) { ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL)) } remainderFunction(x=c(23:47),d=3) I've never seen an ifelse statement used that way, and I was fully expecting that to NOT work, or to place the output of which(x%%d==0) in each location where the statement x%%d==0 was true. Any ideas on deconstructing this? --j Hi Jonathan, While this has already been answered, the question was "why does it work?". As Bill Dunlap pointed out, it is because the "return" does not allow the ifelse to complete. That was not a problem for the student, for it did do what was requested. It is just an unnecessary elaboration of the code, for: remainderFunction<-function(x,d) { which(x%%d==0) } works just as well. I think Bill was pointing out the the order of evaluation was important, for: remainderFunction<-function(x,d) { which(return(x%%d==0)) } doesn't work. The student probably deserves a Rube Goldberg award. Jim __ R-help@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.
Re: [R] ifelse question (I'm not sure why this is working)...
On Sep 10, 2013, at 12:40 PM, Jonathan Greenberg wrote: > R-helpers: > > One of my intrepid students came up with a solution to a problem where > they need to write a function that takes a vector x and a "scalar" d, > and return the indices of the vector x where x %% d is equal to 0 (x > is evenly divisible by d). I thought I had a good handle on the > potential solutions, but one of my students sent me a function that > WORKS, but for the life of me I can't figure out WHY. Here is the > solution: > > remainderFunction<-function(x,d) > { > ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL)) > } > remainderFunction(x=c(23:47),d=3) > > I've never seen an ifelse statement used that way, and I was fully > expecting that to NOT work, or to place the output of which(x%%d==0) > in each location where the statement x%%d==0 was true. I think it did what you expected (at east your second expectation). Look at: c(NULL, 1,2,3, NULL, 4,5,6, NULL) # [1] 1 2 3 4 5 6 Obviously teh ifelse is not needed since this is cleaner code: remainderFunction<-function(x,d) { which(x%%d==0) } remainderFunction(x=c(23:47),d=3) # [1] 2 5 8 11 14 17 20 23 -- David. > > Any ideas on deconstructing this? > > --j > > -- > Jonathan A. Greenberg, PhD > Assistant Professor > Global Environmental Analysis and Remote Sensing (GEARS) Laboratory > Department of Geography and Geographic Information Science > University of Illinois at Urbana-Champaign > 607 South Mathews Avenue, MC 150 > Urbana, IL 61801 > Phone: 217-300-1924 > http://www.geog.illinois.edu/~jgrn/ > AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 > > __ > R-help@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. David Winsemius Alameda, CA, USA __ R-help@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.
Re: [R] ifelse question (I'm not sure why this is working)...
On Sep 10, 2013, at 23:39 , Jim Lemon wrote: > On 09/11/2013 05:40 AM, Jonathan Greenberg wrote: >> R-helpers: >> >> One of my intrepid students came up with a solution to a problem where >> they need to write a function that takes a vector x and a "scalar" d, >> and return the indices of the vector x where x %% d is equal to 0 (x >> is evenly divisible by d). I thought I had a good handle on the >> potential solutions, but one of my students sent me a function that >> WORKS, but for the life of me I can't figure out WHY. Here is the >> solution: >> >> remainderFunction<-function(x,d) >> { >>ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL)) >> } >> remainderFunction(x=c(23:47),d=3) >> >> I've never seen an ifelse statement used that way, and I was fully >> expecting that to NOT work, or to place the output of which(x%%d==0) >> in each location where the statement x%%d==0 was true. >> >> Any ideas on deconstructing this? >> >> --j >> > Hi Jonathan, > While this has already been answered, the question was "why does it work?". > As Bill Dunlap pointed out, it is because the "return" does not allow the > ifelse to complete. That was not a problem for the student, for it did do > what was requested. It is just an unnecessary elaboration of the code, for: > > remainderFunction<-function(x,d) { > which(x%%d==0) > } > > works just as well. I think Bill was pointing out the the order of evaluation > was important, for: > > remainderFunction<-function(x,d) { > which(return(x%%d==0)) > } > > doesn't work. The student probably deserves a Rube Goldberg award. It's slightly trickier: the yes= part of an ifelse is not evaluated unless at least one element of the test is TRUE, so I think the equivalence is closer to remainderFunction<-function(x,d) if (any(x %% d == 0) ) which(x %% d==0) except that ifelse() has the curious feature of returning logical(0) if the test vector is logical(0) (evaluating neither of its arguments). (If you want to be really picky, the above returns NULL _invisibly_ if the any() condition fails. Whether or not the student deserves a little credit for thinking about that case is up in the air, I think. He/she may have failed to grasp the meaning of integer(0).) > > Jim > > __ > R-help@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. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@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.
Re: [R] ifelse question (I'm not sure why this is working)...
Here is the same issue in a simpler form: > f <- function(x) log(return(x)) > f(10) [1] 10 f's 'x' is returned before log does anything with it. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of William Dunlap > Sent: Tuesday, September 10, 2013 12:59 PM > To: Jonathan Greenberg; r-help > Subject: Re: [R] ifelse question (I'm not sure why this is working)... > > > remainderFunction<-function(x,d) > > { > >ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL)) > > } > > remainderFunction(x=c(23:47),d=3) > > The above call returns c(2, 5, 8, 11, 14, 17, 20, 23), the value of > (23:47)%%3. > Note that remainderFunction(integer(0), 3) returns logical(0). > > The return() calls in the call to ifelse cause the problem. The one used > for the second argument to ifelse causes remainderFunction to return, > abandoning the evaluation of ifself, as soon as ifelse evaluates its second > argument. > > I think that using return this way is bad practice, but have seen it in code > like >tryCatch(return(something), > error=function(e)"Error in something") > which is common in support code for RStudio. > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > > > -Original Message- > > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > > Behalf > > Of Jonathan Greenberg > > Sent: Tuesday, September 10, 2013 12:40 PM > > To: r-help > > Subject: [R] ifelse question (I'm not sure why this is working)... > > > > R-helpers: > > > > One of my intrepid students came up with a solution to a problem where > > they need to write a function that takes a vector x and a "scalar" d, > > and return the indices of the vector x where x %% d is equal to 0 (x > > is evenly divisible by d). I thought I had a good handle on the > > potential solutions, but one of my students sent me a function that > > WORKS, but for the life of me I can't figure out WHY. Here is the > > solution: > > > > remainderFunction<-function(x,d) > > { > >ifelse(x%%d==0,yes=return(which(x%%d==0)),no=return(NULL)) > > } > > remainderFunction(x=c(23:47),d=3) > > > > I've never seen an ifelse statement used that way, and I was fully > > expecting that to NOT work, or to place the output of which(x%%d==0) > > in each location where the statement x%%d==0 was true. > > > > Any ideas on deconstructing this? > > > > --j > > > > -- > > Jonathan A. Greenberg, PhD > > Assistant Professor > > Global Environmental Analysis and Remote Sensing (GEARS) Laboratory > > Department of Geography and Geographic Information Science > > University of Illinois at Urbana-Champaign > > 607 South Mathews Avenue, MC 150 > > Urbana, IL 61801 > > Phone: 217-300-1924 > > http://www.geog.illinois.edu/~jgrn/ > > AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 > > > > __ > > R-help@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. > > __ > R-help@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. __ R-help@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.