Dear R-helpers, I need to generate all the binary sequences of length n (here n = 8) that start with 1 and have no fewer than two of each digit, and are not cyclic permutations of each other. Here is what I have done: len <- 8 df <- as.data.frame(numeric(2^(len - 1)) %o% numeric(len)) require(partitions) for (i in 1:2^(len - 1)) df[i, ] <- binary(i, dim = len)[[1]] df <- df[which(df[, 1] == 1), ] df <- df[which(rowSums(df) > 1), ] df <- df[which(rowSums(df) < len - 1), ] The following are cyclic permutations of each other: df[which(rowSums(df) == 3), ][c(2, 15), ] I would like to retain the larger of the two (if considered a binary number). Can someone suggest an algorithm? Thanks, Michael _____________________________ Professor Michael Kubovy University of Virginia Department of Psychology USPS: P.O.Box 400400 Charlottesville, VA 22904-4400 Parcels: Room 102 Gilmer Hall McCormick Road Charlottesville, VA 22903 Office: B011 +1-434-982-4729 Lab: B019 +1-434-982-4751 Fax: +1-434-982-4766 WWW: http://www.people.virginia.edu/~mk9y/ [[alternative HTML version deleted]]
Michael Kubovy
2009-May-05 13:37 UTC
[R] Find cyclically identical binary sequences (corrected)
Dear R-helpers, I need to generate all the binary sequences of length n (here n = 8) that start with 1 and have no fewer than two of each digit, and are not cyclic permutations of each other. Here is what I have done: len <- 8 df <- as.data.frame(numeric(2^(len - 1)) %o% numeric(len)) require(wle) for (i in 1:2^(len - 1)) df[i, ] <- binary(i, dim = len)[[1]] df <- df[which(df[, 1] == 1), ] df <- df[which(rowSums(df) > 1), ] df <- df[which(rowSums(df) < len - 1), ] The following are cyclic permutations of each other: df[which(rowSums(df) == 3), ][c(2, 15), ] I would like to retain the larger of the two (if considered a binary number). Can someone suggest an algorithm? Thanks, Michael _____________________________ Professor Michael Kubovy University of Virginia Department of Psychology USPS: P.O.Box 400400 Charlottesville, VA 22904-4400 Parcels: Room 102 Gilmer Hall McCormick Road Charlottesville, VA 22903 Office: B011 +1-434-982-4729 Lab: B019 +1-434-982-4751 Fax: +1-434-982-4766 WWW: http://www.people.virginia.edu/~mk9y/