mercredi 18 août 2021

How do I find the biggest value in a for loop?

I want to build a function in R that takes in a set of numbers and subjects them to two tests: it should determine whether they are bigger than a certain value "a" and which one of those are the biggest among them. This is an example of what I've got so far:

some_numbers <- c(2,7,8,100,43,6,14)

biggest_number_test <- function(numbers, n) {
for (i in 1:n)
  if(numbers[i] > a = 5)
return(max(numbers[i]))
}

But this only returns the first value that fits both requirements:

> biggest_number_test(some_numbers)
[1] 7

But I want it to return the value of, in this case, 100. How do I do that?

Aucun commentaire:

Enregistrer un commentaire