[R] for-loop ?

Christian Schulz ozric at web.de
Sat Dec 7 00:32:02 CET 2002


...why the loop produce NA's ?

>>healthy(25.47252)
[1] 0.764
>>healthy(26)
[1] 0.5

many thanks,christian

healthy <- function (x) {
+        if(x < 18)
+        {res <- 0 }
+        if(x >= 18 & x <= 20)
+        { res <- ((20-x)/2) }
+        if(x > 20 & x < 25)
+        { res <- 1 }
+        if(x >= 25 & x <= 27)
+         { res <- ((27-x)/2)}
+        if(x > 27) { res <- 0}
+              return(res)
+        }
>>
>>test[i] <- for (i in 1:30) {
+       healthy(i)
+       }
>>
>>test
 [1]  0 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
NA
[26] NA NA NA NA  0




More information about the R-help mailing list