Created
December 8, 2014 07:02
-
-
Save leoluyi/8f1670d582c03b3df362 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
multi.freq.table = function(data, sep="+", dropzero=FALSE, clean=TRUE) { | |
## 共選分析 | |
# Takes boolean multiple-response data and tabulates it according | |
# to the possible combinations of each variable. | |
# | |
# See: http://stackoverflow.com/q/11348391/1270695 | |
counts = data.frame(table(data)) | |
N = ncol(counts) | |
counts$Combn = apply(counts[-N] == 1, 1, | |
function(x) paste(names(counts[-N])[x], | |
collapse=sep)) | |
if (isTRUE(dropzero)) { | |
counts = counts[counts$Freq != 0, ] | |
} else if (!isTRUE(dropzero)) { | |
counts = counts | |
} | |
if (isTRUE(clean)) { | |
counts = data.frame(Combn = counts$Combn, Freq = counts$Freq) | |
} | |
counts | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment