Consider the following code:
A <- matrix(1:12, ncol=4)
colnames(A) <- letters[1:4]
class(A) <- c("foo", "matrix")
when A is subset, it loses the "foo" class label:
class(A[1:2,])
# [1] "matrix"
The same happens with vectors. Yet, the same doesn't happen with data.frames:
B <- as.data.frame(A)
class(B) <- c("foo", "data.frame")
class(B[1:2,])
# [1] "foo" "data.frame"
And usually, applying generic functions to objects preserves the class attribute. Not for matrix/numeric/integer objects. Why? And can this behavior be avoided?