3
I have labels stored as letters and want to convert to numerics, for example:
a <- c("A", "B", "A", "A", "C", "E", "D", "A", "B")
Desired exit
[1] 1 2 1 1 3 5 4 1 2
3
I have labels stored as letters and want to convert to numerics, for example:
a <- c("A", "B", "A", "A", "C", "E", "D", "A", "B")
Desired exit
[1] 1 2 1 1 3 5 4 1 2
5
Transform your vector a
in factor and then convert it to numeric:
as.numeric(factor(a))
[1] 1 2 1 1 3 5 4 1 2
It is even possible to assign different numerical values to the levels of a
. For example,
as.numeric(factor(a, levels=c("E", "D", "C", "B", "A")))
[1] 5 4 5 5 3 1 2 5 4
Browser other questions tagged r
You are not signed in. Login or sign up in order to post.