[R] ifelse question (I'm not sure why this is working)...

David Winsemius dwinsemius at comcast.net
Wed Sep 11 02:21:29 CEST 2013


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: jgrn307 at hotmail.com, Gchat: jgrn307, Skype: jgrn3007
> 
> ______________________________________________
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list