Calculate min and max of a vector using for loop.

edited December 2018 in R

I want to make a program which calculates min and max of a vector using for loops , here is my code

a <- c(24,57,111,454,86) b<-0 c<-0 for(i in a) { if(a>i) { b<-d[i]
print("Max")
} else if (a>i) { c<-a[i]
print("min")
}}

and gives me [1] "max" ...
what is wrong here?

Tagged:

Comments

  • Try this

    for(i in a){

    • if(all(a <= i)){
    • b <- i
    • print(paste0("max = ", b))
    • }else if(all(a >= i)) {
    • c <- i
    • print(paste0("min = ", c))
    • }}
Sign In or Register to comment.