繁体   English   中英

为什么我为R矢量的类看到“整数”而不是“矢量”

[英]Why do I see “integer” rather than “Vector” for the class of an R vector

为什么从数据框中切出的列的数据类型显示为“整数”而不是“向量”?

df <- data.frame(x = 1:3, y = c('a', 'b', 'c'))
#  x y
#1 1 a
#2 2 b
#3 3 c
c1 <- df[ ,1]
#[1] 1 2 3
class(c1)
#[1] "integer"

在R中,“class”是对象的属性。 但是,在R语言定义中,向量不能具有除“名称”之外的其他属性(这实际上是“因子”不是向量的原因)。 这里的函数class给出了向量的“模式”。

来自?vector

 ‘is.vector’ returns ‘TRUE’ if ‘x’ is a vector of the specified
 mode having no attributes _other than names_.  It returns ‘FALSE’
 otherwise.

?class

 Many R objects have a ‘class’ attribute, a character vector giving
 the names of the classes from which the object _inherits_.  If the
 object does not have a class attribute, it has an implicit class,
 ‘"matrix"’, ‘"array"’ or the result of ‘mode(x)’ (except that
 integer vectors have implicit class ‘"integer"’).

有关矢量的“模式”,请参阅此处 ,了解另一个令人惊讶的R对象: NULL

要了解“因素”问题,请尝试第二列:

c2 <- df[, 2]

attributes(c2)
#$levels
#[1] "a" "b" "c"
#
#$class
#[1] "factor"

class(c2)
#[1] "factor"

is.vector(c2)
#[1] FALSE

因为那是类型。 它是integer s的vector :)

看到

?vector

?integer

~J

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM