'Getting "comparison of these types is not implemented" error while trying to compare two numbers in a list
I have a bubble sort function that looks like this:
print_sorted_array <- function(arr)
{
len <- length(arr)
for (i in 1:len){
j <- i
while((j > 1)){
if ((arr[j] < arr[j-1])){
temp <- arr[j]
arr[j] <- arr[j-1]
arr[j-1] <- temp
}
j <- j - 1
}
}
return(arr)
}
When I try to call this function
print_sorted_array(list(5, 16, 8, 2))
I get this error:
Error in arr[j] < arr[j - 1] :
comparison of these types is not implemented
I tried debugging it but couldn't figure out the RStudio debugger as I'm very new to this. I would appreciate any help, thanks!
Solution 1:[1]
Nevermind, using as.integer() on both list elements in the if statement worked. Still don't know why it wasn't integer in the first place but stopped caring.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jetamo |
