R:将字符变量编码为数字 [英] R: Encode character variables into numeric
本文介绍了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
这篇关于R:将字符变量编码为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文