0

I have a block of code like this:

while(i < 35)
{ 
  m<-strsplit(datalist[i],split=",")
  m<-sapply(m,as.numeric)
  m<-c(m)
  dif<- m-eingabe

  mnear<-sum(dif^2)

  if(mnear < near)
  {
    near<-mnear
    position<-i
  }
  i<-i+1
}
i

my problem is that WHILE count only until 8 and not until 35. If I commnet if statement It works correct till 35. ( I need this If statement in While) How can I solve this problem?

UPDATE

I am getting this warning message:

Error in if (mnear < near) { : missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In lapply(X = X, FUN = FUN, ...) : NAs introduced by coercion
2: In m - eingabe :
  longer object length is not a multiple of shorter object length
Kaja
  • 2,962
  • 18
  • 63
  • 99
  • This doesn't answer your question, but take it into account, you define `position<-i`, but then you don't use `position` check that out. – Jilber Urbina Jan 08 '14 at 13:39
  • Looks fine to me, add a `i <- 1` before the loop and a `print(i)` inside of it, it should loop up to `i == 34`. – flodel Jan 08 '14 at 13:40
  • @Jilber, `near` and `position` must be part of the output: the minimum distance and the index at which it was reached. – flodel Jan 08 '14 at 13:43
  • @flodel If I add `i<-1` before the loop, it counts only untill 8 – Kaja Jan 08 '14 at 13:48
  • 4
    You need to supply a **reproducible** example. My bet is that you're getting an `NA` for `mnear` when `i` is 8 or 9, and your code is terminating. Have you failed to mention any error or warning messages? – Carl Witthoft Jan 08 '14 at 14:28
  • @CarlWitthoftI have updated my post – Kaja Jan 08 '14 at 14:38
  • In addition to the error/warning messages we need the `datalist` and `eingabe` object, see [this link](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for info on how to do that. – Paul Hiemstra Jan 08 '14 at 15:09
  • I note with minor satisfaction :-) that the error message indicates my first comment was exactly correct. Now you need to backtrack and find out what "bad" values you are extracting from your `datalist` . – Carl Witthoft Jan 08 '14 at 15:15
  • Thank zou guys for your help, I have found it. an `NA` in datalist was the problem – Kaja Jan 08 '14 at 15:52
  • 1
    You can deal with that by doing `datalist<-datalist[!is.na(datalist)]` prior to running your loop. – Carl Witthoft Jan 08 '14 at 16:28

0 Answers0