Skip to content

Instantly share code, notes, and snippets.

@leoluyi
Created December 8, 2014 07:02
Show Gist options
  • Save leoluyi/8f1670d582c03b3df362 to your computer and use it in GitHub Desktop.
Save leoluyi/8f1670d582c03b3df362 to your computer and use it in GitHub Desktop.
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