So, I have been looking at some code in R where the person mistakenly put as.numeric to convert a factor to a numeric value. I have already fixed this issue, but it got me wondering what does as.numeric actually do to a factor. I have been looking through the code and it seems to randomly choose what numbers to leave out. Here is some example code:
d <- c(10,10,2,3,5,29,1, 1.5, 0.5,3,4)
max_value <- c(4,4,4,4,5,5,2,2,2,2,2)
max_value <- as.factor(max_value)
d <= as.numeric(max_value)
d[d <= as.numeric(max_value)]
I would assume the as.numeric of a factor would give the number of levels. However, this code has a factor with 3 levels and only keeps 2, 1, and 0.5. If it did use the number 3 as the max level, then it should also keep 3, 1.5, and 3. What is going on?