# How to change a value to zero, using a loop?

Solution for How to change a value to zero, using a loop?
is Given Below:

The below loop is used to prepare data before I perform survival analyses. `Vector_B` should be turned to ‘0’ based on the maximum value (= days) in `Vector_A`. In this example I want the maximum value to be ’10’. I already know how to use the ifelse-function for this, but I really want to know how to implement loops instead (in this case a for loop).

Could you help me out with the script below? I want `Vector_B` to change to ”0, 0, 1, 0, 0, 0”) because at every `Vector_A` higher than 10, `Vector_B` should be ‘0’.

Code:

``````Vector_A <- c(7, 17, 5, 12, 7, 13)
Vector_B <- c(0, 1, 1, 0, 0, 1)
for (i in Vector_A) if (i > 10) {(Vector_B <- 0)}
``````

Outcome:

Vector_A: 7 17 5 12 7 13

Vector_B: 0

This is quite an odd way of doing things as there exist R function already for this but seeing as you explicitly asked for a loop try this.

``````for (i in 1:length(Vector_A)){
Vector_B[i] <- ifelse(Vector_A[i] > 10, 0 ,Vector_B[i])
}
``````