Using Ben Bolker's excellent answer above, I have created a short function that will do this for any data frame containing one column with success counts, one column for failure counts, and any number of additional columns that contain information about each row (subject). See example below.
#####################################################################
### cnt2bin (count to binary) takes a data frame with 2-column ######
### "count" response variable of successes and failures and ######
### converts it to long format, with one column showing ######
### 0s and 1s for failures and successes. ######
### data is data frame with 2-column response variable ######
### suc and fail are character expressions for columns ######
### containing counts of successes and failures respectively ######
#####################################################################
cnt2bin <- function(data, suc, fail) {
xvars <- names(data)[names(data)!=suc & names(data)!=fail]
list <- lapply(xvars, function(z) with(data, rep(get(z), get(suc)+get(fail))))
names(list) <- xvars
df <- as.data.frame(list)
with(data,data.frame(bin=rep(rep(c(1,0),nrow(data)),c(rbind(get(suc),get(fail)))),
df))
}
Example, where id is the subject id, s and f are columns counting successes and failures for each subject, and x and y are variables that describe attributes of each subject, to be expanded and added to the final data frame.
dd <- read.table(text="id s f x y
1 0 3 A A
2 2 1 A B
3 1 2 B B",
header=TRUE)
cnt2bin(dd, "s", "f")