Inspired by Andry, you can use identical and avoid any problems with attributes, using the fact that it is an empty set of this object class and combine it with an element of this class:
attr(a,"foo")<-"bar" > identical(1L,c(a,1L)) [1] TRUE
Or more generally:
is.empty <- function(x, mode=NULL){ if (is.null(mode)) mode <- class(x) identical(vector(mode,1),c(x,vector(class(x),1))) } b <- numeric(0) > is.empty(a) [1] TRUE > is.empty(a,"numeric") [1] FALSE > is.empty(b) [1] TRUE > is.empty(b,"integer") [1] FALSE
James Jun 23 '11 at 11:11 2011-06-23 11:11
source share