Reputation: 1192
Suppose you have a list where each element is an integer(0). How can you catch this "error" in an effective way?
# Reproducible example
set.seed(10)
a <- list(a = as.factor(integer(0)), b = as.factor(sample(letters[1:5], 10, rep=T)))
levels(a$a) <- levels(a$b)
a$b <- as.factor(integer(0))
levels(a$b) <- levels(a$a)
a
# I know I can catch an integer(0) using length()
length(integer(0))
# But how can I check that all list elements are integer(0)???
The only case I'd like to catch is the one where all the elements of the list are integer(0).
Upvotes: 0
Views: 45
Reputation: 146030
This will check that the length of every list element is 0:
all(lengths(a) == 0)
It won't differentiate between numeric(0)
, integer(0)
, character(0)
, etc.
If the integer
part important, You might have some like in your example), but you could make another check: all(sapply(a, typeof) == "integer")
if you only want integer(0)
s.
Upvotes: 3