R:将字符变量编码为数字

问题描述:

在R代码中,我有一个字符变量 var ,其值是 AA, AB, AC等。

In R code I have a character variable var that has values "AA", "AB", "AC", etc.

str(var)
chr [1:17003] "AA" "AA" "AA" "AA" "AB" "AB" ...

如何将其转换为数字变量,以便将 AA编码为例如1, AB-等于2,依此类推。

How can I convert it to numeric variable so that "AA" would be coded as, e.g. 1, "AB" - as 2, etc.

您可以将字符串转换为因子,然后转换为数字。

You can convert the string to a factor and then to numeric.

x <- c("AA", "AB", "AB", "AC", "AA", "XY")
as.numeric(as.factor(x))
# [1] 1 2 2 3 1 4

或者,您可以使用 match unique

match(x, unique(x))
# [1] 1 2 2 3 1 4