R All possible sub-combinations -
i have data this:
basin <- c("volta","seine","limpopo") c1 <- c("ben","bel","sa") c2 <- c("burk","fra","moz") c3 <- c("ivc","lux","zim") c4 <- c("gha","na","bots") c5 <- c("mali","na","na") c6 <- c("togo","na","na") df <- data.frame(basin, c1, c2, c3, c4, c5, c6) basin c1 c2 c3 c4 c5 c6 1 volta ben bur ivc gha mali togo 2 seine bel fra lux na na na 3 limpopo sa moz zim bots na na
each basin has k countries. example in first row, need generate combinations of 5 countries, combinations of 4 countries, , on. second row need generate combinations of 2 countries , third row combinations of 3 countries , 2countries. add these subsets new rows in data.
i tried use function:
allsubs <- function(x, k) { if(k > length(x)) stop('k > length(x)') if(choose(length(x), k)==1){ list(as.vector(combn(x, k))) } else { cbn <- combn(x, k) lapply(seq(ncol(cbn)), function(i) cbn[,i]) } }
however, can work if feed data this:
allsubs(c('ben','burk','ivc','gha','mali','togo'),4)
but need iterate through rows in data frame. appreciate help.
here 1 solution data follows (your input data still bit problematic , modified code generate correct data):
basin <- c("volta","seine","limpopo") c1 <- c("ben","bel","sa") c2 <- c("burk","fra","moz") c3 <- c("ivc","lux","zim") c4 <- c("gha",na,"bots") c5 <- c("mali",na,na) c6 <- c("togo",na,na) df <- data.frame(basin, c1, c2, c3, c4, c5, c6, stringsasfactors = false) basin c1 c2 c3 c4 c5 c6 1 volta ben burk ivc gha mali togo 2 seine bel fra lux <na> <na> <na> 3 limpopo sa moz zim bots <na> <na> apply(df[, 2:7], 1, function(x) {l <- x[!is.na(x)]; sapply(seq(1:(length(l)-1)), function(y) combn(l, y))})
result various sub combinations of each country code per row. hope helps. of course, can split 'inline' function(s) used external functions invoke in apply
call.
Comments
Post a Comment