Convert factor to integer

This question already has an answer here:

  • How to convert a factor to integernumeric without loss of information? 5 answers

  • You can combine the two functions; coerce to characters thence to numerics:

    > fac <- factor(c("1","2","1","2"))
    > as.numeric(as.character(fac))
    [1] 1 2 1 2
    

    Quoting directly from the help page for factor :

    To transform a factor f to its original numeric values, as.numeric(levels(f))[f] is recommended and slightly more efficient than as.numeric(as.character(f)) .

    链接地址: http://www.djcxy.com/p/24942.html

    上一篇: 将数据框中的因子列转换为数字类型列

    下一篇: 将因子转换为整数