Re: [R] odd feature

2006-05-22 Thread Martin Maechler
 Gabor == Gabor Grothendieck [EMAIL PROTECTED]
 on Sun, 21 May 2006 09:47:07 -0400 writes:

Gabor If you know that test is a scalar

Gabor result - if (test) a else b

Gabor will do it.  

Yes, indeed!
IMO,  ifelse(test, a, b) is much overused where as  
  if(test) a else b  is much UNDER used.

From some e-mail postings, and even some documents (even printed
books?), I get the impression that too many people think that
 ifelse(.,.,.)  is to be used as expression / function and 
  if(.) . else . only for program flow control.
This leads to quite suboptimal code, and I personally use
if(.) . else .  __as expression__ much more frequently than ifelse(.,.,.)
 
Martin Maechler, ETH Zurich.

Gabor Here is another approach:

Gabor as.vector(test * ts(a) + (!test) * ts(b))



Gabor On 5/21/06, ivo welch [EMAIL PROTECTED] wrote:
 Dear R wizards:
 
 I just got stung by the ifelse() feature.
 
  a - 10:15
  b - 20:300
  test - 1
  ifelse(test,a,b)
 [1] 10
 
 I had not realized that this was the default behavior---I had expected
 10:15.  mea culpa.  however, I wonder whether it would make sense to
 replace ifelse with a different semantic, where if test is a single
 scalar, it means what a stupid user like me would imagine.
 
 Aside, I like the flexibility of R, but I am not thrilled by all the
 recycling rules.  I either mean I want a scalar or a vector of
 equal/appropriate dimension.  I never want a recycle of a smaller
 vector.  (I do often use a recycle of a scalar.)
 
 regards,
 
 /iaw
 
 __
 R-help@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
 

Gabor __
Gabor R-help@stat.math.ethz.ch mailing list
Gabor https://stat.ethz.ch/mailman/listinfo/r-help
Gabor PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html

__
R-help@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


Re: [R] odd feature

2006-05-22 Thread Duncan Murdoch
On 5/22/2006 3:26 AM, Martin Maechler wrote:
 Gabor == Gabor Grothendieck [EMAIL PROTECTED]
 on Sun, 21 May 2006 09:47:07 -0400 writes:
 
 Gabor If you know that test is a scalar
 
 Gabor result - if (test) a else b
 
 Gabor will do it.  
 
 Yes, indeed!
 IMO,  ifelse(test, a, b) is much overused where as  
   if(test) a else b  is much UNDER used.
 
From some e-mail postings, and even some documents (even printed
 books?), I get the impression that too many people think that
  ifelse(.,.,.)  is to be used as expression / function and 
   if(.) . else . only for program flow control.
 This leads to quite suboptimal code, and I personally use
 if(.) . else .  __as expression__ much more frequently than ifelse(.,.,.)

For overuse of ifelse(), do you mean cases where test is length 1, so 
if() would work?  Or are you thinking of something else?

I'd also be interested in what you mean by quite suboptimal code.  Are 
you thinking of things like

  if (test)
 temp - a
  else
 temp - b
  result - f(temp)

versus

  result - f( if (test) a else b )

?

I would generally use the former, because it's easier to get the 
formatting right, and I find it easier to read.  It's suboptimal in 
speed and memory use because of creating the temp variable, but in most 
cases I think that would be such a small difference that the small 
increase in readability is worthwhile.

Duncan Murdoch

  
 Martin Maechler, ETH Zurich.
 
 Gabor Here is another approach:
 
 Gabor as.vector(test * ts(a) + (!test) * ts(b))
 
 
 
 Gabor On 5/21/06, ivo welch [EMAIL PROTECTED] wrote:
  Dear R wizards:
  
  I just got stung by the ifelse() feature.
  
   a - 10:15
   b - 20:300
   test - 1
   ifelse(test,a,b)
  [1] 10
  
  I had not realized that this was the default behavior---I had expected
  10:15.  mea culpa.  however, I wonder whether it would make sense to
  replace ifelse with a different semantic, where if test is a single
  scalar, it means what a stupid user like me would imagine.
  
  Aside, I like the flexibility of R, but I am not thrilled by all the
  recycling rules.  I either mean I want a scalar or a vector of
  equal/appropriate dimension.  I never want a recycle of a smaller
  vector.  (I do often use a recycle of a scalar.)
  
  regards,
  
  /iaw
  
  __
  R-help@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
  
 
 Gabor __
 Gabor R-help@stat.math.ethz.ch mailing list
 Gabor https://stat.ethz.ch/mailman/listinfo/r-help
 Gabor PLEASE do read the posting guide! 
 http://www.R-project.org/posting-guide.html
 
 __
 R-help@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

__
R-help@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


Re: [R] odd feature

2006-05-22 Thread Frank E Harrell Jr
Duncan Murdoch wrote:
 On 5/22/2006 3:26 AM, Martin Maechler wrote:
 
Gabor == Gabor Grothendieck [EMAIL PROTECTED]
on Sun, 21 May 2006 09:47:07 -0400 writes:

Gabor If you know that test is a scalar

Gabor result - if (test) a else b

Gabor will do it.  

Yes, indeed!
IMO,  ifelse(test, a, b) is much overused where as  
  if(test) a else b  is much UNDER used.

From some e-mail postings, and even some documents (even printed
books?), I get the impression that too many people think that
 ifelse(.,.,.)  is to be used as expression / function and 
  if(.) . else . only for program flow control.
This leads to quite suboptimal code, and I personally use
if(.) . else .  __as expression__ much more frequently than ifelse(.,.,.)
 
 
 For overuse of ifelse(), do you mean cases where test is length 1, so 
 if() would work?  Or are you thinking of something else?
 
 I'd also be interested in what you mean by quite suboptimal code.  Are 
 you thinking of things like
 
   if (test)
  temp - a
   else
  temp - b
   result - f(temp)
 
 versus
 
   result - f( if (test) a else b )
 
 ?
 
 I would generally use the former, because it's easier to get the 
 formatting right, and I find it easier to read.  It's suboptimal in 
 speed and memory use because of creating the temp variable, but in most 
 cases I think that would be such a small difference that the small 
 increase in readability is worthwhile.

IMHO that approach too verbose and not more readable.

Frank

 
 Duncan Murdoch
 
 
 
Martin Maechler, ETH Zurich.

Gabor Here is another approach:

Gabor as.vector(test * ts(a) + (!test) * ts(b))



Gabor On 5/21/06, ivo welch [EMAIL PROTECTED] wrote:
 Dear R wizards:
 
 I just got stung by the ifelse() feature.
 
  a - 10:15
  b - 20:300
  test - 1
  ifelse(test,a,b)
 [1] 10
 
 I had not realized that this was the default behavior---I had expected
 10:15.  mea culpa.  however, I wonder whether it would make sense to
 replace ifelse with a different semantic, where if test is a single
 scalar, it means what a stupid user like me would imagine.
 
 Aside, I like the flexibility of R, but I am not thrilled by all the
 recycling rules.  I either mean I want a scalar or a vector of
 equal/appropriate dimension.  I never want a recycle of a smaller
 vector.  (I do often use a recycle of a scalar.)
 
 regards,
 
 /iaw
 
 __
 R-help@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
 



-- 
Frank E Harrell Jr   Professor and Chair   School of Medicine
  Department of Biostatistics   Vanderbilt University

__
R-help@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


Re: [R] odd feature

2006-05-22 Thread Gabor Grothendieck
If you don't like f(if (temp) a else b)
then what about

temp - if (test) a else b
f(temp)

or

temp - if (test)
  a
else
  b
f(temp)

I think its easier to understand if you factor the temp- out since
one immediately then knows the purpose of the statement is
to set temp.


On 5/22/06, Duncan Murdoch [EMAIL PROTECTED] wrote:
 On 5/22/2006 3:26 AM, Martin Maechler wrote:
  Gabor == Gabor Grothendieck [EMAIL PROTECTED]
  on Sun, 21 May 2006 09:47:07 -0400 writes:
 
  Gabor If you know that test is a scalar
 
  Gabor result - if (test) a else b
 
  Gabor will do it.
 
  Yes, indeed!
  IMO,  ifelse(test, a, b) is much overused where as
if(test) a else b  is much UNDER used.
 
 From some e-mail postings, and even some documents (even printed
  books?), I get the impression that too many people think that
   ifelse(.,.,.)  is to be used as expression / function and
if(.) . else . only for program flow control.
  This leads to quite suboptimal code, and I personally use
  if(.) . else .  __as expression__ much more frequently than ifelse(.,.,.)

 For overuse of ifelse(), do you mean cases where test is length 1, so
 if() would work?  Or are you thinking of something else?

 I'd also be interested in what you mean by quite suboptimal code.  Are
 you thinking of things like

  if (test)
 temp - a
  else
 temp - b
  result - f(temp)

 versus

  result - f( if (test) a else b )

 ?

 I would generally use the former, because it's easier to get the
 formatting right, and I find it easier to read.  It's suboptimal in
 speed and memory use because of creating the temp variable, but in most
 cases I think that would be such a small difference that the small
 increase in readability is worthwhile.

 Duncan Murdoch

 
  Martin Maechler, ETH Zurich.
 
  Gabor Here is another approach:
 
  Gabor as.vector(test * ts(a) + (!test) * ts(b))
 
 
 
  Gabor On 5/21/06, ivo welch [EMAIL PROTECTED] wrote:
   Dear R wizards:
  
   I just got stung by the ifelse() feature.
  
a - 10:15
b - 20:300
test - 1
ifelse(test,a,b)
   [1] 10
  
   I had not realized that this was the default behavior---I had 
  expected
   10:15.  mea culpa.  however, I wonder whether it would make sense to
   replace ifelse with a different semantic, where if test is a single
   scalar, it means what a stupid user like me would imagine.
  
   Aside, I like the flexibility of R, but I am not thrilled by all the
   recycling rules.  I either mean I want a scalar or a vector of
   equal/appropriate dimension.  I never want a recycle of a smaller
   vector.  (I do often use a recycle of a scalar.)
  
   regards,
  
   /iaw
  
   __
   R-help@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
  
 
  Gabor __
  Gabor R-help@stat.math.ethz.ch mailing list
  Gabor https://stat.ethz.ch/mailman/listinfo/r-help
  Gabor PLEASE do read the posting guide! 
  http://www.R-project.org/posting-guide.html
 
  __
  R-help@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

 __
 R-help@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


__
R-help@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


Re: [R] odd feature

2006-05-22 Thread Duncan Murdoch
On 5/22/2006 9:38 AM, Frank E Harrell Jr wrote:
 Duncan Murdoch wrote:
 On 5/22/2006 3:26 AM, Martin Maechler wrote:

 Gabor == Gabor Grothendieck [EMAIL PROTECTED]
on Sun, 21 May 2006 09:47:07 -0400 writes:
Gabor If you know that test is a scalar

Gabor result - if (test) a else b

Gabor will do it.  

 Yes, indeed!
 IMO,  ifelse(test, a, b) is much overused where as  
  if(test) a else b  is much UNDER used.

 From some e-mail postings, and even some documents (even printed
 books?), I get the impression that too many people think that
 ifelse(.,.,.)  is to be used as expression / function and 
  if(.) . else . only for program flow control.
 This leads to quite suboptimal code, and I personally use
 if(.) . else .  __as expression__ much more frequently than ifelse(.,.,.)

 For overuse of ifelse(), do you mean cases where test is length 1, so 
 if() would work?  Or are you thinking of something else?

 I'd also be interested in what you mean by quite suboptimal code.  Are 
 you thinking of things like

   if (test)
  temp - a
   else
  temp - b
   result - f(temp)

 versus

   result - f( if (test) a else b )

 ?

 I would generally use the former, because it's easier to get the 
 formatting right, and I find it easier to read.  It's suboptimal in 
 speed and memory use because of creating the temp variable, but in most 
 cases I think that would be such a small difference that the small 
 increase in readability is worthwhile.
 
 IMHO that approach too verbose and not more readable.

IMO terse unreadable :-)

Duncan Murdoch

 
 Frank
 
 Duncan Murdoch


 Martin Maechler, ETH Zurich.

Gabor Here is another approach:

Gabor as.vector(test * ts(a) + (!test) * ts(b))



Gabor On 5/21/06, ivo welch [EMAIL PROTECTED] wrote:
 Dear R wizards:
 
 I just got stung by the ifelse() feature.
 
  a - 10:15
  b - 20:300
  test - 1
  ifelse(test,a,b)
 [1] 10
 
 I had not realized that this was the default behavior---I had expected
 10:15.  mea culpa.  however, I wonder whether it would make sense to
 replace ifelse with a different semantic, where if test is a single
 scalar, it means what a stupid user like me would imagine.
 
 Aside, I like the flexibility of R, but I am not thrilled by all the
 recycling rules.  I either mean I want a scalar or a vector of
 equal/appropriate dimension.  I never want a recycle of a smaller
 vector.  (I do often use a recycle of a scalar.)
 
 regards,
 
 /iaw
 
 __
 R-help@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
 

 


__
R-help@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


Re: [R] odd feature

2006-05-22 Thread Martin Maechler
 Gabor == Gabor Grothendieck [EMAIL PROTECTED]
 on Mon, 22 May 2006 09:31:14 -0400 writes:

Gabor If you don't like f(if (temp) a else b)
Gabor then what about

Gabor temp - if (test) a else b
Gabor f(temp)

Gabor or

Gabor temp - if (test)
Gabor a
Gabor else
Gabor b
Gabor f(temp)

Gabor I think its easier to understand if you factor the temp- out since
Gabor one immediately then knows the purpose of the statement is
Gabor to set temp.

I strongly agree with Gabor on the above.

But, to Duncan's question: 
Yes, indeed, my main point was that people use  
ifelse(test, a, b)  also in cases where test is known to be of
length one.

BTW, the 2nd point about why I don't ``like'' ifelse() so much
is on its help page:

Both 'a' and 'b' are fully evaluated even though only one of the
two values of a[i], b[i] are used in the result.

Martin

Gabor On 5/22/06, Duncan Murdoch [EMAIL PROTECTED] wrote:
 On 5/22/2006 3:26 AM, Martin Maechler wrote:
  Gabor == Gabor Grothendieck [EMAIL PROTECTED]
  on Sun, 21 May 2006 09:47:07 -0400 writes:
 
  Gabor If you know that test is a scalar
 
  Gabor result - if (test) a else b
 
  Gabor will do it.
 
  Yes, indeed!
  IMO,  ifelse(test, a, b) is much overused where as
if(test) a else b  is much UNDER used.
 
 From some e-mail postings, and even some documents (even printed
  books?), I get the impression that too many people think that
   ifelse(.,.,.)  is to be used as expression / function and
if(.) . else . only for program flow control.
  This leads to quite suboptimal code, and I personally use
  if(.) . else .  __as expression__ much more frequently than 
ifelse(.,.,.)
 
 For overuse of ifelse(), do you mean cases where test is length 1, so
 if() would work?  Or are you thinking of something else?
 
 I'd also be interested in what you mean by quite suboptimal code.  Are
 you thinking of things like
 
 if (test)
 temp - a
 else
 temp - b
 result - f(temp)
 
 versus
 
 result - f( if (test) a else b )
 
 ?
 
 I would generally use the former, because it's easier to get the
 formatting right, and I find it easier to read.  It's suboptimal in
 speed and memory use because of creating the temp variable, but in most
 cases I think that would be such a small difference that the small
 increase in readability is worthwhile.
 
 Duncan Murdoch
 
 
  Martin Maechler, ETH Zurich.
 
  Gabor Here is another approach:
 
  Gabor as.vector(test * ts(a) + (!test) * ts(b))
 
 
 
  Gabor On 5/21/06, ivo welch [EMAIL PROTECTED] wrote:
   Dear R wizards:
  
   I just got stung by the ifelse() feature.
  
a - 10:15
b - 20:300
test - 1
ifelse(test,a,b)
   [1] 10
  
   I had not realized that this was the default behavior---I had 
expected
   10:15.  mea culpa.  however, I wonder whether it would make 
sense to
   replace ifelse with a different semantic, where if test is a 
single
   scalar, it means what a stupid user like me would imagine.
  
   Aside, I like the flexibility of R, but I am not thrilled by 
all the
   recycling rules.  I either mean I want a scalar or a vector of
   equal/appropriate dimension.  I never want a recycle of a 
smaller
   vector.  (I do often use a recycle of a scalar.)
  
   regards,
  
   /iaw
  
   __
   R-help@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
  
 
  Gabor __
  Gabor R-help@stat.math.ethz.ch mailing list
  Gabor https://stat.ethz.ch/mailman/listinfo/r-help
  Gabor PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html
 
  __
  R-help@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
 
 __
 R-help@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


__
R-help@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


Re: [R] odd feature

2006-05-22 Thread Gabor Grothendieck
Due to lazy evaluation, I don't think a and b are fully evaluated:

 ifelse(1, a - 1, b - 2)
[1] 1
 a
[1] 1
 b
Error: object b not found


On 5/22/06, Martin Maechler [EMAIL PROTECTED] wrote:
  Gabor == Gabor Grothendieck [EMAIL PROTECTED]
  on Mon, 22 May 2006 09:31:14 -0400 writes:

Gabor If you don't like f(if (temp) a else b)
Gabor then what about

Gabor temp - if (test) a else b
Gabor f(temp)

Gabor or

Gabor temp - if (test)
Gabor a
Gabor else
Gabor b
Gabor f(temp)

Gabor I think its easier to understand if you factor the temp- out since
Gabor one immediately then knows the purpose of the statement is
Gabor to set temp.

 I strongly agree with Gabor on the above.

 But, to Duncan's question:
 Yes, indeed, my main point was that people use
 ifelse(test, a, b)  also in cases where test is known to be of
 length one.

 BTW, the 2nd point about why I don't ``like'' ifelse() so much
 is on its help page:

 Both 'a' and 'b' are fully evaluated even though only one of the
 two values of a[i], b[i] are used in the result.

 Martin

Gabor On 5/22/06, Duncan Murdoch [EMAIL PROTECTED] wrote:
 On 5/22/2006 3:26 AM, Martin Maechler wrote:
  Gabor == Gabor Grothendieck [EMAIL PROTECTED]
  on Sun, 21 May 2006 09:47:07 -0400 writes:
 
  Gabor If you know that test is a scalar
 
  Gabor result - if (test) a else b
 
  Gabor will do it.
 
  Yes, indeed!
  IMO,  ifelse(test, a, b) is much overused where as
if(test) a else b  is much UNDER used.
 
 From some e-mail postings, and even some documents (even printed
  books?), I get the impression that too many people think that
   ifelse(.,.,.)  is to be used as expression / function and
if(.) . else . only for program flow control.
  This leads to quite suboptimal code, and I personally use
  if(.) . else .  __as expression__ much more frequently than 
 ifelse(.,.,.)

 For overuse of ifelse(), do you mean cases where test is length 1, so
 if() would work?  Or are you thinking of something else?

 I'd also be interested in what you mean by quite suboptimal code.  Are
 you thinking of things like

 if (test)
 temp - a
 else
 temp - b
 result - f(temp)

 versus

 result - f( if (test) a else b )

 ?

 I would generally use the former, because it's easier to get the
 formatting right, and I find it easier to read.  It's suboptimal in
 speed and memory use because of creating the temp variable, but in most
 cases I think that would be such a small difference that the small
 increase in readability is worthwhile.

 Duncan Murdoch

 
  Martin Maechler, ETH Zurich.
 
  Gabor Here is another approach:
 
  Gabor as.vector(test * ts(a) + (!test) * ts(b))
 
 
 
  Gabor On 5/21/06, ivo welch [EMAIL PROTECTED] wrote:
   Dear R wizards:
  
   I just got stung by the ifelse() feature.
  
a - 10:15
b - 20:300
test - 1
ifelse(test,a,b)
   [1] 10
  
   I had not realized that this was the default behavior---I had 
 expected
   10:15.  mea culpa.  however, I wonder whether it would make 
 sense to
   replace ifelse with a different semantic, where if test is a 
 single
   scalar, it means what a stupid user like me would imagine.
  
   Aside, I like the flexibility of R, but I am not thrilled by 
 all the
   recycling rules.  I either mean I want a scalar or a vector of
   equal/appropriate dimension.  I never want a recycle of a 
 smaller
   vector.  (I do often use a recycle of a scalar.)
  
   regards,
  
   /iaw
  
   __
   R-help@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
  
 
  Gabor __
  Gabor R-help@stat.math.ethz.ch mailing list
  Gabor https://stat.ethz.ch/mailman/listinfo/r-help
  Gabor PLEASE do read the posting guide! 
 http://www.R-project.org/posting-guide.html
 
  __
  R-help@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

 __
 R-help@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




Re: [R] odd feature

2006-05-22 Thread Thomas Lumley
On Mon, 22 May 2006, Gabor Grothendieck wrote:

 Due to lazy evaluation, I don't think a and b are fully evaluated:

 ifelse(1, a - 1, b - 2)
 [1] 1
 a
 [1] 1
 b
 Error: object b not found

yes. If you look at the code for ifelse() it evaluates the second argument 
if any test values are TRUE and the third argument if any test values are 
FALSE, so in the scalar case it does not evaluate both arguments.

-thomas


Thomas Lumley   Assoc. Professor, Biostatistics
[EMAIL PROTECTED]   University of Washington, Seattle

__
R-help@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


Re: [R] odd feature

2006-05-22 Thread Thomas Lumley
On Sun, 21 May 2006, ivo welch wrote:

 Aside, I like the flexibility of R, but I am not thrilled by all the
 recycling rules.  I either mean I want a scalar or a vector of
 equal/appropriate dimension.  I never want a recycle of a smaller
 vector.  (I do often use a recycle of a scalar.)


One case where the vector-vector recycling rules are used is in 
vector-matrix operations:
a-1:4
b-diag(4)
a+b

-thomas

__
R-help@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


Re: [R] odd feature

2006-05-22 Thread Martin Maechler
 TL == Thomas Lumley [EMAIL PROTECTED]
 on Mon, 22 May 2006 07:09:09 -0700 (PDT) writes:

TL On Mon, 22 May 2006, Gabor Grothendieck wrote:
 Due to lazy evaluation, I don't think a and b are fully evaluated:
 
 ifelse(1, a - 1, b - 2)
 [1] 1
 a
 [1] 1
 b
 Error: object b not found

TL yes. If you look at the code for ifelse() it evaluates
TL the second argument if any test values are TRUE and the
TL third argument if any test values are FALSE, so in the
TL scalar case it does not evaluate both arguments.

yes.  
And (Gabor), if you recall what I said earlier,  it's pretty clear
that I would not even *consider* using ifelse() in the scalar
case.  So my original statement was about the case ifelse() is
designed for: The non-trivial vector case, and there my
statement of fully evaluated  does apply.

Martin

__
R-help@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


Re: [R] odd feature

2006-05-22 Thread Gabor Grothendieck
On 5/22/06, Martin Maechler [EMAIL PROTECTED] wrote:
  TL == Thomas Lumley [EMAIL PROTECTED]
  on Mon, 22 May 2006 07:09:09 -0700 (PDT) writes:

TL On Mon, 22 May 2006, Gabor Grothendieck wrote:
 Due to lazy evaluation, I don't think a and b are fully evaluated:

 ifelse(1, a - 1, b - 2)
 [1] 1
 a
 [1] 1
 b
 Error: object b not found

TL yes. If you look at the code for ifelse() it evaluates
TL the second argument if any test values are TRUE and the
TL third argument if any test values are FALSE, so in the
TL scalar case it does not evaluate both arguments.

 yes.
 And (Gabor), if you recall what I said earlier,  it's pretty clear
 that I would not even *consider* using ifelse() in the scalar
 case.  So my original statement was about the case ifelse() is
 designed for: The non-trivial vector case, and there my
 statement of fully evaluated  does apply.

 Martin


I don't think they are fully evaluated in the vector case either:

 ifelse(rep(TRUE,4), a - 1:4, b - 1:4)
[1] 1 2 3 4
 a
[1] 1 2 3 4
 b
Error: object b not found

__
R-help@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


Re: [R] odd feature

2006-05-22 Thread Uwe Ligges
Gabor Grothendieck wrote:

 On 5/22/06, Martin Maechler [EMAIL PROTECTED] wrote:
 
TL == Thomas Lumley [EMAIL PROTECTED]
on Mon, 22 May 2006 07:09:09 -0700 (PDT) writes:

   TL On Mon, 22 May 2006, Gabor Grothendieck wrote:
Due to lazy evaluation, I don't think a and b are fully evaluated:
   
ifelse(1, a - 1, b - 2)
[1] 1
a
[1] 1
b
Error: object b not found

   TL yes. If you look at the code for ifelse() it evaluates
   TL the second argument if any test values are TRUE and the
   TL third argument if any test values are FALSE, so in the
   TL scalar case it does not evaluate both arguments.

yes.
And (Gabor), if you recall what I said earlier,  it's pretty clear
that I would not even *consider* using ifelse() in the scalar
case.  So my original statement was about the case ifelse() is
designed for: The non-trivial vector case, and there my
statement of fully evaluated  does apply.

Martin

 
 
 I don't think they are fully evaluated in the vector case either:
 
 
ifelse(rep(TRUE,4), a - 1:4, b - 1:4)
 
 [1] 1 2 3 4
 
a
 
 [1] 1 2 3 4
 
b
 
 Error: object b not found


Martin's non-trivial means not all conditions are TRUE or not all are 
FALSE, then both statements are FULLY evaluated and he is right.

See the ifelse() code which is not that hard to read...

Uwe



 __
 R-help@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

__
R-help@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


Re: [R] odd feature

2006-05-22 Thread Martin Maechler
 Gabor == Gabor Grothendieck [EMAIL PROTECTED]
 on Mon, 22 May 2006 11:34:09 -0400 writes:

Gabor On 5/22/06, Martin Maechler [EMAIL PROTECTED] wrote:
  TL == Thomas Lumley [EMAIL PROTECTED]
  on Mon, 22 May 2006 07:09:09 -0700 (PDT) writes:
 
TL On Mon, 22 May 2006, Gabor Grothendieck wrote:
  Due to lazy evaluation, I don't think a and b are fully evaluated:
 
  ifelse(1, a - 1, b - 2)
  [1] 1
  a
  [1] 1
  b
  Error: object b not found
 
TL yes. If you look at the code for ifelse() it evaluates
TL the second argument if any test values are TRUE and the
TL third argument if any test values are FALSE, so in the
TL scalar case it does not evaluate both arguments.
 
 yes.
 And (Gabor), if you recall what I said earlier,  it's pretty clear
 that I would not even *consider* using ifelse() in the scalar
 case.  So my original statement was about the case ifelse() is
 designed for: The non-trivial vector case, and there my
 statement of fully evaluated  does apply.
 
 Martin
 

Gabor I don't think they are fully evaluated in the vector case either:

 ifelse(rep(TRUE,4), a - 1:4, b - 1:4)
Gabor [1] 1 2 3 4
 a
Gabor [1] 1 2 3 4
 b
Gabor Error: object b not found

PLEASE 
--

I said  non-trivial  above 
Martin

__
R-help@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


Re: [R] odd feature

2006-05-22 Thread ivo welch
Hi Thomas:

 One case where the vector-vector recycling rules are used is in
 vector-matrix operations:
 a-1:4
 b-diag(4)
 a+b

is this last expression intended to be intuitive and thus desirable?
if anything else, I would end up writing something like this more as
an error than by intent.  I would suggest that recycling might be
better to be explicitly asked for by the user---though I am not sure
what the syntax should be.
   allow.recycle(a+b)
at the end, I think implicit recycling is a tradeoff between
convenience and unintended errors.  I would judge the convenience of
implicit recycling to be fairly low, and the unintended error rate
[with difficulty finding it] to be fairly high.

I guess the R language has few options() that control its
behavior---e.g., ala use strict; in perl.  If it did, I would love
to turn off implicit recycling, provided there is an explicit recycle
possibility.  I would not mind having the ability to be forced to
define variables first, either, though this is not a big deal.  R is
pretty good in telling me when I use variables that have not been
defined.

regards,

/ivo




 -thomas


__
R-help@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


Re: [R] odd feature

2006-05-22 Thread Duncan Murdoch
On 5/22/2006 5:01 PM, ivo welch wrote:
 Hi Thomas:
 
 One case where the vector-vector recycling rules are used is in
 vector-matrix operations:
 a-1:4
 b-diag(4)
 a+b
 
 is this last expression intended to be intuitive and thus desirable?
 if anything else, I would end up writing something like this more as
 an error than by intent.  I would suggest that recycling might be
 better to be explicitly asked for by the user---though I am not sure
 what the syntax should be.
allow.recycle(a+b)
 at the end, I think implicit recycling is a tradeoff between
 convenience and unintended errors.  I would judge the convenience of
 implicit recycling to be fairly low, and the unintended error rate
 [with difficulty finding it] to be fairly high.
 
 I guess the R language has few options() that control its
 behavior---e.g., ala use strict; in perl.  If it did, I would love
 to turn off implicit recycling, provided there is an explicit recycle
 possibility.  

While I agree with you about these things in principle, this is a very 
old S design decision, and I think it can't really change now.  It would 
have to be a local change (i.e. in my namespace, do things strictly), 
and that's very unlikely to happen.

I would not mind having the ability to be forced to
  define variables first, either, though this is not a big deal.  R is
  pretty good in telling me when I use variables that have not been
  defined.

There are some code tools around that help to detect such things.

Duncan Murdoch

__
R-help@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


[R] odd feature

2006-05-21 Thread ivo welch
Dear R wizards:

I just got stung by the ifelse() feature.

 a - 10:15
 b - 20:300
 test - 1
 ifelse(test,a,b)
[1] 10

I had not realized that this was the default behavior---I had expected
10:15.  mea culpa.  however, I wonder whether it would make sense to
replace ifelse with a different semantic, where if test is a single
scalar, it means what a stupid user like me would imagine.

Aside, I like the flexibility of R, but I am not thrilled by all the
recycling rules.  I either mean I want a scalar or a vector of
equal/appropriate dimension.  I never want a recycle of a smaller
vector.  (I do often use a recycle of a scalar.)

regards,

/iaw

__
R-help@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


Re: [R] odd feature

2006-05-21 Thread Gabor Grothendieck
If you know that test is a scalar

result - if (test) a else b

will do it.  Here is another approach:

as.vector(test * ts(a) + (!test) * ts(b))



On 5/21/06, ivo welch [EMAIL PROTECTED] wrote:
 Dear R wizards:

 I just got stung by the ifelse() feature.

  a - 10:15
  b - 20:300
  test - 1
  ifelse(test,a,b)
 [1] 10

 I had not realized that this was the default behavior---I had expected
 10:15.  mea culpa.  however, I wonder whether it would make sense to
 replace ifelse with a different semantic, where if test is a single
 scalar, it means what a stupid user like me would imagine.

 Aside, I like the flexibility of R, but I am not thrilled by all the
 recycling rules.  I either mean I want a scalar or a vector of
 equal/appropriate dimension.  I never want a recycle of a smaller
 vector.  (I do often use a recycle of a scalar.)

 regards,

 /iaw

 __
 R-help@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


__
R-help@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


Re: [R] odd feature

2006-05-21 Thread Frank E Harrell Jr
ivo welch wrote:
 Dear R wizards:
 
 I just got stung by the ifelse() feature.
 
 
a - 10:15
b - 20:300
test - 1
ifelse(test,a,b)
 
 [1] 10
 
 I had not realized that this was the default behavior---I had expected
 10:15.  mea culpa.  however, I wonder whether it would make sense to
 replace ifelse with a different semantic, where if test is a single
 scalar, it means what a stupid user like me would imagine.
 
 Aside, I like the flexibility of R, but I am not thrilled by all the
 recycling rules.  I either mean I want a scalar or a vector of
 equal/appropriate dimension.  I never want a recycle of a smaller
 vector.  (I do often use a recycle of a scalar.)
 
 regards,
 
 /iaw

The current behavior is logical to me.  Are you looking for if(test) a 
else b?
Frank
-- 
Frank E Harrell Jr   Professor and Chair   School of Medicine
  Department of Biostatistics   Vanderbilt University

__
R-help@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