Skip to content
Prev 138675 / 398503 Next

FOR LOOP w/IFELSE statement help

On 05-Mar-08 23:37:42, zack holden wrote:
I would rather do this in the following kind of way:
### Example 1:
xx <- c(3,4,5,5,6,7,8,9)
nn <- length(xx)
if(any(xx[1:(nn-1)]==xx[2:nn])){
  min(which(xx[1:(nn-1)]==xx[2:nn]))
  } else max(xx)
### Result: [1] 3

### Example 2:
x <- c(3,4,5,6,7,8,9,10)
n <- length(x)
if(any(x[1:(n-1)]==x[2:n])){
  min(which(x[1:(n-1)]==x[2:n]))
  } else max(x)
### Result: [1] 10

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 05-Mar-08                                       Time: 23:45:05
------------------------------ XFMail ------------------------------