Alternatively, using attributes ( see @CathG answer ), you can use attr . The first will work with NULL objects, and the second will not. When you work with R attributes, you should remember that they are not simple as they look, and can have some interesting side effects. Quick example:
> x <- 1:10 > class(x) [1] "integer" > x [1] 1 2 3 4 5 6 7 8 9 10
So far so good. Now you can set the attribute dim
> attr(x, 'dim') <- c(2, 5) > class(x) [1] "matrix" > x [,1] [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10
class attribute is a fundamental part of S3 classes :
> foo <- list() > foo list()
Let's see what happens if we set the class attribute to 'data.frame'
> attr(foo, 'class') <- 'data.frame' > foo data frame with 0 columns and 0 rows
or we can define custom behavior (BTW this behavior is the reason that it is better to avoid dots when defining functions):
> print.foo <- function(x) cat("I'm foo\n") > attr(foo, 'class') <- 'foo' > foo I'm foo
Other attributes, such as comment and names , also have special meanings and limitations. Remove the message here, you need to be a little careful when working with attributes in R. One simple idea to deal with is to use prefixes as artificial namespaces:
> x <- 1:10 > attr(x, 'zero323.dim') <- c(2, 5) > class(x) [1] "integer" > x [1] 1 2 3 4 5 6 7 8 9 10 attr(, 'zero323.dim') [1] 2 5
In my opinion, this is especially useful when using third-party libraries. The use of attributes is usually poorly documented, especially when used for some internal tasks, and it is quite easy to introduce some difficulties for diagnosing errors if you use conflicting names.