I am in a strange fix here. I am using data.table
for a very routine task, but there is something that I am not able to explain. I have figured out a way around
The correct way to convert from factor
to numeric
or integer
is to go through character
. This is because internally, a factor is an integer index (that refers to a levels
vector). When you tell R to convert it to numeric
it will simply convert the underlying index, not try to convert the level label.
Short answer: do Cases:=as.numeric(as.character(Cases))
.
Edit: Alternatively the ?factor
help page suggests as.numeric(levels(Cases))[Cases]
as more efficient. h/t @Gsee in the comments.