I have a vector that contains 100 random values between 0 and 100 called "aVector". I need to create a loop that finds the minimum value within aVector using "if" statements and comparison operators (>, <, ==, etc.). I can't use min() or max() functions in or outside of the loop.
What I have so far (aVector is setup but my loop doesn't work):
set.seed
aVector <- sample(0:100, 100, replace=FALSE)
for (i in 1:(aVector)) {
if(aVector[i] < 1)
1 = aVector[i]
}
This should do the trick. First create a variable called "low" with a high value (as @alistaire suggested). Then, loop through your values of aVector. At each value, check if the value is less than low. If it is, update low to that value.
set.seed(42)
aVector <- sample(0:100, 100, replace=FALSE)
low <- Inf # initialize with high value
for (i in aVector) {
if(i < low){
low <- i
}
}
print(low)
# Confirm we got correct answer
min(aVector)