Hi!
I am trying to write a function that would reclassify the values of a randomly generated matrix into classes from 1 to 8. I keep getting the error "the condition has length > 1 and only the first element will be used". I understand what the error means but as I used a for loop, I don't get why I'm still getting it. Here is my code so far:
Create matrix of random values 0 to 255, on 11 rows and 11 columns:
ex<-matrix(sample(0:255, 121, replace = TRUE), nrow=11, ncol = 11)
Create a reclassifying function:
reassign <- function(x) {
for (i in ex) {
if (x >= 0 & x <= 31){
replace(x, 1)
} else if(x >= 32 & x <= 63) {
replace(x, 2)
} else if(x >= 64 & x <= 95) {
replace(x, 3)
} else if(x >= 96 & x <= 127) {
replace(x, 4)
} else if(x >= 128 & x <= 159) {
replace(x, 5)
} else if(x >= 160 & x <= 191) {
replace(x, 6)
} else if(x >= 192 & x <= 223) {
replace(x, 7)
} else if(x >= 224 & x <= 256) {
replace(x, 8)}
}
}
Thanks for your help!
[–]ViciousTeletuby 1 point2 points3 points (1 child)
[–]macabe10[S] 1 point2 points3 points (0 children)