I'm returning to R after some time, and the following has me stumped:
I'd like to build a list of the positions factor values have in the facor levels list. Example:
> data = c("a", "b", "a","a","c")
> fdata = factor(data)
> fdata
[1] a b a a c
Levels: a b c
> fdata$lvl_idx <- ????
Such that:
> fdata$lvl_idx
[1] 1 2 1 1 3
Appreciate any hints or tips.
The solution provided years ago by Matthew Lundberg is not robust. It could be that the
as.integer()
function was defined for a specific S3 type of factors. Imagine someone would create a new factor class to keep operators like>=
.Now this is not working anymore, - it just removes operators:
But this is robust with any factor you want to know the index of the level of, using
which()
: