Skip to content

for-loop ?

2 messages · Christian Schulz, Deepayan Sarkar

#
...why the loop produce NA's ?
[1] 0.764
[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)
+        }
+       healthy(i)
+       }
[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
#
On Friday 06 December 2002 05:27 pm, Christian Schulz wrote:
You probably want to do


test <- numeric(30)

for (i in 1:30) {
    test[i] <- healthy(i)
}

-Deepayan