オブジェクトのclass
は、そのオブジェクトの要素のストレージ モードと同じである必要はありません。のヘルプページからclass
:
多くの R オブジェクトには class 属性があります。これは、オブジェクトが継承するクラスの名前を指定する文字ベクトルです。オブジェクトがクラス属性を持たない場合は、暗黙のクラス、"matrix"、"array"、または mode(x) の結果を持ちます (整数ベクトルには暗黙のクラス "integer" がある場合を除く)。
したがって、次のようにします。
y <- as.difftime( c(0.5,1,2), units='mins' )
# 'typeof' determines the (R internal) type or storage mode of any object
typeof( y )
# [1] "double"
# And mode: Modes have the same set of names as types (see typeof) except that
# types "integer" and "double" are returned as "numeric".
mode( y )
# [1] "numeric"
class
は、次のようなことを可能にするプログラミング構造です。
# Integer vector
x <- 1:5
# Set the class with 'class<-'
class(x) <- "foo"
# Which is correctly returned by 'class'
class(x)
# [1] "foo"
# But our elements are still integers...
typeof(x)
[1] "integer"