I have an assignment to sort 3 numbers in a vector by R only using if, else statements. I have changed parenthesis quite a bit but continue to get the "unexpected else in else" error in console
x <- c(200, 700, 1000)
if ((x[1] > x[2]) & (x[1] > x[3])) {
if (x[2] > x[3]) {
ord <- c(x[1], x[2], x[3])
} else {
ord <- c(x[1], x[3], x[2])
}
}
else if ((x[2] > x[1]) & (x[2] > x[3]) {
if (x[1] > x[3]) {
ord <- c(x[2], x[1], x[3])
} else {
ord <- c(x[2], x[3], x[1])
}
}
else if {
(x[1] > x[2])
ord <- c(x[3], x[1], x[2])
} else {
ord <- c(x[3], x[2], x[2])
}
print(ord)
Aucun commentaire:
Enregistrer un commentaire