Suppose I have a vector or column of arbitrary length representing some grouping/factor variable with an arbitrary number of groups and arbitrary values for same along the lines of this:
a <- c(2,2,2,2,2,7,7,7,7,10,10,10,10,10)
a
[1] 2 2 2 2 2 7 7 7 7 10 10 10 10 10
How would I most easily turn that into this:
a
[1] 1 1 1 1 1 2 2 2 2 3 3 3 3 3
a <- c(2,2,2,2,2,7,7,7,7,10,10,10,10,10)
c(factor(a))
#[1] 1 1 1 1 1 2 2 2 2 3 3 3 3 3
Explanation:
A factor is just an integer vector with levels attribute and a class attribute. c removes attributes as a side effect. You could use as.numeric or as.integer instead of c with similar or the same results, respectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With