В R "класс" является атрибутом объекта. Однако в определении языка 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