【问题标题】:Why is the type integer? [duplicate]为什么是整数类型? [复制]
【发布时间】:2019-12-26 20:04:33
【问题描述】:

当这显然是一个字符串时,为什么 typeof() 会产生一个整数?

> Str_IN$V1[trapper]
    [1] IT1*0000010*660*LB*4.07*PE*BP*2018710000*MP*12345*VN*944940
    20 Levels: BEG*20140905*93608602*20140730*45056564 CTT*1 DTM*011*20140904 ... TDS*280120

    > typeof(Str_IN$V1[trapper])
    [1] "integer"

【问题讨论】:

  • factor,存储模式是integer。试试as.integer(factor(c("A", "B", "C")) 你可能正在寻找class(Str_IN$V1[trapper])
  • 您可以使用 class 而不是 typeof 来获取有关向量的更多信息(尽管它并不总是返回长度为 1 的向量...在许多情况下,它会返回一个长度为 2 或更大的向量,如 class(Sys.time())class(as_tibble(mtcars)))。
  • 为什么存储模式是整数?更好地理解这一点的任何参考可能会有所帮助...@r2evans

标签: r dataframe typeof


【解决方案1】:

根据?typeof

typeof 确定任何对象的(R 内部)类型或存储方式

OP的列是factor,内部存储模式是integer。可以通过mode查看

mode(factor("A"))
#[1] "numeric"

及其storage 版本

storage.mode(factor("A"))
#[1] "integer"

相反,我们可能需要检查class

class(Str_IN$V1[trapper])

【讨论】:

    猜你喜欢
    • 2012-03-14
    • 1970-01-01
    • 2011-07-06
    • 2019-08-10
    • 2013-07-20
    • 2018-10-15
    • 2015-02-18
    • 2014-09-10
    • 1970-01-01
    相关资源
    最近更新 更多