2

I have a data set with 31557 observations, and the variables Order.number and Materials. I'm trying to run this in R:

First:

DT <- data.table(Order.number=X$Order.number, Materials=X$Materials)
setorder(DT, Order.number, Materials)

Then:

library(data.table)    
ans <- DT[, as.data.table(do.call(rbind, combn(Materials, 2, simplify=FALSE))), 
      by=Order.number][,
                       .N, by=.(V1, V2)]

But I get the error in combn(Materials, 2, simplify = FALSE) : n < m

It works if I just use random generated table. So could it be something to do with the dataset I have?

EDIT: I tried with meaning of combn error, but getting "Error in do.call(rbind, function(x) if (length(x) > 1) { : second argument must be a list"

ans <- DT[, as.data.table(do.call(rbind, function(x)
  if(length(x)>1) {
    combn(Materials, 2, simplify=FALSE)
  }
  else x)), 
  by=Order.number][,
  .N, by=.(V1, V2)]
smci
  • 32,567
  • 20
  • 113
  • 146
M.A
  • 65
  • 1
  • 8
  • What is `length(Materials)` returning? – Rui Barradas May 23 '18 at 18:36
  • Do you mean length(DT$Materials) ? It is returning 31557 – M.A May 23 '18 at 18:45
  • Possible duplicate of [meaning of combn error: Error in FUN(X\[\[i\]\], ...) : n < m in R](https://stackoverflow.com/questions/36570399/meaning-of-combn-error-error-in-funxi-n-m-in-r) – CER May 23 '18 at 19:01
  • No one that can help? – M.A May 23 '18 at 19:48
  • In your `if` it should be `if(length(x)>1) { combn(x, 2, simplify=FALSE) }`. – Rui Barradas May 23 '18 at 21:05
  • Still doesn't work. It says: Error in do.call(rbind, function(x) if (length(x) > 1) { : second argument must be a list – M.A May 23 '18 at 21:26
  • 1
    Clearly you have some value of grouping variable `Order.number` in your DT giving a group of length 1 or less, hence `combn(Materials, 2...)` complains that n < m. You can easily diagnose which group has length 1 with `DT[, .N, by=Order.number] [N==1]` – smci May 24 '18 at 04:52
  • Feel free to upvote answers you found useful – smci May 28 '18 at 06:10

2 Answers2

2

Clearly you have some value of grouping variable Order.number in your DT giving a group of length 1 or less, hence combn(Materials, 2...) complains that n < m.

You can easily diagnose which group has length 1 with DT[, .N, by=Order.number] [N==1].

Then either exclude those from your summary, or write a wrapper for combn that does nothing when the input length n < m.

(Arguably combn should have an enhance non-default option to selectively squelch the error, when applied to groups of length n < 2, as is likely to happen when run on a grouped df/dt)

smci
  • 32,567
  • 20
  • 113
  • 146
0

Do you have some NA values in Materials?

Otherwise your code works for me

#generate some random data
X<-data.frame(Order.number=rep(letters[1:10],3), Materials=rep(letters[11:20],3))

library(data.table)
DT <- data.table(Order.number=X$Order.number, Materials=X$Materials)
setorder(DT, Order.number, Materials)

ans <- DT[, as.data.table(do.call(rbind, combn(Materials, 2, simplify=FALSE))), 
      by=Order.number][,
                       .N, by=.(V1, V2)]
Esther
  • 1,115
  • 1
  • 10
  • 15