I have some data that look like 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. For example in the first row, I need to generate all combinations of 5 countries, all combinations of 4 countries, and so on. The second row I need to generate all combinations of 2 countries and third row all combinations of 3 countries and 2countries. Then I would like to add these subsets as new rows in the data.

I tried to use this 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, I can only get it to work if I feed it the data like this:

```
allSubs(c('BEN','BURK','IVC','GHA','MALI','TOGO'),4)

```

But I really need it to iterate through the rows in the data frame. I appreciate any help.