Re: [R] test for nested factors

From: hadley wickham <>
Date: Mon, 04 Jun 2007 15:29:09 +0200

On 6/4/07, Tim Bergsma <> wrote:
> Is there a conventional way to test for nested factors? I.e., if 'a'
> and 'b' are lists of same-length factors, does each level specified by
> 'a' correspond to exactly one level specified by 'b'?
> The function below seems to suffice, but I'd be happy to know of a more
> succinct solution, if it already exists.

How about:

"%nested%" <- function(a, b) {

	if (is.list(a)) a <-"interaction", c(a, drop=TRUE))
	if (is.list(b)) b <-"interaction", c(b, drop=TRUE))
	length(unique(a))  == length(unique(interaction(a, b, drop=TRUE)))

CO2$Plant %nested% CO2[,c("Type","Treatment")] #TRUE CO2$Plant %nested% (CO2$uptake < mean(CO2$uptake)) #FALSE


Hadley mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 04 Jun 2007 - 13:36:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 04 Jun 2007 - 14:31:50 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.