[R] Switch and integer

2006-12-11 Thread Knut Krueger
I searched the help list and the manual, but I do not find my mistake.
Switch is working with character , with integer, but not in the third 
example

Regards Knut


count1 - 0
test 
=c(3,9,3,9,3,9,8,9,8,9,8,9,3,9,3,9,5,9,3,9,1,2,7,9,3,9,1,1,2,2,3,9,2,1,2,5,9,8,9,1,2)
count1 - 0
for (i in 1:length(test))
  switch (EXPR=test[i],
  5 = print(test[i]),
  6 = print(test[i]),
  7 = print(test[i]),
  8 = print(test[i]),
  9 = print(test[i])
  )
  count1
# example from helpfile
for(i in c(-1:3,9))  print(switch(i, 1,2,3,4))

#  not working
test 
=c(3,9,3,9,3,9,8,9,8,9,8,9,3,9,3,9,5,9,3,9,1,2,7,9,3,9,1,1,2,2,3,9,2,1,2,5,9,8,9,1,2)
count1 - 0
for (i in 1:length(test))
  switch (EXPR=test[i],
  4 = count1 - count1 +1,
  5 = count1 - count1 +1,
  6 = count1 - count1 +1,
  7 = count1 - count1 +1,
  8 = count1 - count1 +1
  )
  count1

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Switch and integer

2006-12-11 Thread David Barron
From the help entry for switch:

If the value of EXPR is an integer between 1 and nargs()-1 then the
corresponding element of ... is evaluated and the result returned.

So, for integers you don't give names to the elements in ...  This should work:

test=c(3,9,3,9,3,9,8,9,8,9,8,9,3,9,3,9,5,9,3,9,1,2,7,9,3,9,1,1,2,2,3,9,2,1,2,5,9,8,9,1,2)
count1 - 0
for (i in 1:length(test))
 switch(EXPR=test[i],
 NULL,
 NULL,
 NULL,
 count1 - count1 +1,
 count1 - count1 +1,
 count1 - count1 +1,
 count1 - count1 +1,
 count1 - count1 +1,
 NULL
 )
 count1


On 11/12/06, Knut Krueger [EMAIL PROTECTED] wrote:
 I searched the help list and the manual, but I do not find my mistake.
 Switch is working with character , with integer, but not in the third
 example

 Regards Knut


 count1 - 0
 test
 =c(3,9,3,9,3,9,8,9,8,9,8,9,3,9,3,9,5,9,3,9,1,2,7,9,3,9,1,1,2,2,3,9,2,1,2,5,9,8,9,1,2)
 count1 - 0
 for (i in 1:length(test))
   switch (EXPR=test[i],
   5 = print(test[i]),
   6 = print(test[i]),
   7 = print(test[i]),
   8 = print(test[i]),
   9 = print(test[i])
   )
   count1
 # example from helpfile
 for(i in c(-1:3,9))  print(switch(i, 1,2,3,4))

 #  not working
 test
 =c(3,9,3,9,3,9,8,9,8,9,8,9,3,9,3,9,5,9,3,9,1,2,7,9,3,9,1,1,2,2,3,9,2,1,2,5,9,8,9,1,2)
 count1 - 0
 for (i in 1:length(test))
   switch (EXPR=test[i],
   4 = count1 - count1 +1,
   5 = count1 - count1 +1,
   6 = count1 - count1 +1,
   7 = count1 - count1 +1,
   8 = count1 - count1 +1
   )
   count1

 __
 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
 and provide commented, minimal, self-contained, reproducible code.



-- 
=
David Barron
Said Business School
University of Oxford
Park End Street
Oxford OX1 1HP

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Switch and integer

2006-12-11 Thread Dieter Menne
Knut Krueger Knut-krueger at einthal.de writes:

 
 Switch is working with character , with integer, but not in the third 
 example
... 
 #  not working
test = c(3,9,3,9,3,9,8,9,8,9,8,9,3,9,3,9,5,9,3,9,1,2,7,9,3,9,1,1,
2,2,3,9,2,1,2,5, 9,8,9,1,2)
 count1 - 0
 for (i in 1:length(test))
   switch (EXPR=test[i],
   4 = count1 - count1 +1,
   5 = count1 - count1 +1,
   6 = count1 - count1 +1,
   7 = count1 - count1 +1,
   8 = count1 - count1 +1
   )
   count1
 

switch has a different behavior when an integer is use, which is a bit hidden 
in the term corresponding. 

If the value of EXPR is an integer between 1 and nargs()-1 then the
corresponding element of ... is evaluated and the result returned.

So something like the example below might come close to what you want. 
In each case you have to add the default last item to avoid a NULL.

In the case of no match, if there's a further argument in switch that one 
is returned, otherwise NULL.

Dieter

test =c(4,5,8,1,13)
count1 - 0
for (i in 1:length(test)) {
  count1 - switch (EXPR=as.character(test[i]),
  4 = count1 +1,
  5 = count1 +1,
  6 = count1 +1,
  7 = count1 +1,
  8 = count1 +1,
  count1
  )
}
count1

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Switch and integer

2006-12-11 Thread Knut Krueger
David Barron schrieb:

 If the value of EXPR is an integer between 1 and nargs()-1 then the
 corresponding element of ... is evaluated and the result returned.

Thank you, I wondered about the
 integer between 1 and nargs()-1
and the example from the help file for(i in c(-1:3,9))  print(switch(i,
1,2,3,4))
No see I  that the 1,2,3,4 is the output not the value, but it's not
very clear for me ho it works.
I will try to find it out ...
The switch for Integer is very different from C++ or Pascal .. maybe
this was the reason for the problem.

Regards Knut

__
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
and provide commented, minimal, self-contained, reproducible code.