<p><code>batch = Pheno_LMS$batchId</code> should be <code>batch = Pheno_LMS$BatchId</code>, if you fix that then at least the code you showed works just fine.</p>

CC BY-SA 4.0
2019-12-03T18:27:39.493Z
Thanks @DevonRyan.In addition of your comment I found 2 batches by just 1 sample. So, I removed them from my TCGA_expr_log and now ComBat() runs perfectly.
Mohammad
CC BY-SA 4.0
2019-12-03T08:10:40.947Z
2019-12-03T08:10:40.947Z
Devon Ryan
<p>as suggested in the comment you should verify if the command:</p>

<pre><code>dat[, batch == batch_level]
</code></pre>

<p>returns an empty object or not. Apparently, <em>batch</em> and <em>batch_level</em> do not have anyting that match and the function tries to run over an empty dataframe.</p>

CC BY-SA 4.0
2019-12-02T15:04:36.723Z
Thanks for @gabt comment. in the end of my post, I add "batch" content. As you see, when I call batch, for each sample I see a batch value. i have 14 batches.
Mohammad
CC BY-SA 4.0
2019-12-02T09:30:21.817Z
2019-12-02T09:30:21.817Z
gbt
<p>I used <code>ComBat()</code> for batch effect correction in my expression data. basically, that function inputs are expression data, Batch covariate, and Model matrix for the outcome of interest and other covariates besides batch. So, I prepared all inputs based on the instruction of <code>ComBat()</code> function in 'sva' package. "Pheno_LMS" is my clinical data inclus batchId of my samples and "TCGA_expr_log" is my expression data. my code is below:</p>

<p>Pheno_LMS structure:</p>

<pre><code>Sample        batchId   age  . . . 
GSM71019.CEL    396     63
GSM71020.CEL    396     58
GSM71021.CEL    410     85
GSM71020.CEL    411     58
GSM71021.CEL    410     40
.
.
.
dim(Pheno_LMS)
[1] 74 37
</code></pre>

<p>TCGA_expr_log structure:</p>

<pre><code>                   GSM71019.CEL 1      GSM71019.CEL 2        GSM71019.CEL 3  . . . 
    Gene id          
  ENSG00000000003     10.115170           8.628044                  8.779235  
  ENSG00000000005     5.345168            5.063598                   5.113116
  ENSG00000000009     6.348024            6.663625                   6.465892     
    .
    .
    .
dim(TCGA_expr_log)
[1] 42817    74

batch = Pheno_LMS$batchId
modcombat = model.matrix(~1, data=Pheno_LMS)
TCGA_expr_Co &lt;-  ComBat(as.matrix(TCGA_expr_log),batch = batch,mod = modcombat, par.prior = TRUE,mean.only = TRUE)
</code></pre>

<p>but when I run that function, I got below error:</p>

<pre><code>Error in apply(dat[, batch == batch_level], 1, function(x) { : 
  dim(X) must have a positive length
</code></pre>

<p>Now, I need anybody comments about my problem. thanks</p>

<p>P.S: when i call batch I see:</p>

<pre><code>&gt; batch
 [1] 13 13 13 14 14 14 14 14 14 14 14 6  6  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7 
[29] 7  8  7  8  11 11 11 14 12 1  6  4  2  3  3  3  3  6  6  1  3  9  8  6  11 7  3  5 
[57] 4  5  5  4  4  11 8  8  12 12 12 12 12 10 10 10 11 13
Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14
</code></pre>

CC BY-SA 4.0
2019-12-02T08:30:37.450Z
`dat[, batch == batch_level]` is likely empty.
Devon Ryan
CC BY-SA 4.0
2019-12-02T15:06:17.727Z
Thanks for @DevonRyan comment. at the end of my post, I add "batch" content. As you see, when I call batch, for each sample I see a batch value. I have 14 batches.based on your knowledge how can I solve this problem?
Mohammad
CC BY-SA 4.0
2019-12-02T16:37:02.893Z
You need to show us something that’s reproducible. As is we need to read your mind to figure out exactly what’s going wrong.
Devon Ryan
CC BY-SA 4.0
2019-12-02T21:35:11.263Z
Dear @DevonRyan, for finding problem I save "Pheno_LMS" and "TCGA_expr_log" to "my_data.RData" which you can download it from this link:   
     https://www.dropbox.com/s/is4b9laou305d5k/my_data.RData?dl=0
Mohammad
CC BY-SA 4.0
2019-12-02T21:38:53.743Z
Dear @DevonRyan, I appreciate it if you check my_data.RData and guide me for solving the problem.
Mohammad
<r><batch-effects><combat><sva>
how to solve “dim(X) must have a positive length” at running ComBat function in R
# how to solve “dim(X) must have a positive length” at running ComBat function in R

(Asked by: username_0 on 2019-12-02)

I used ComBat() for batch effect correction in my expression data. basically, that function inputs are expression data, Batch covariate, and Model matrix for the outcome of interest and other covariates besides batch. So, I prepared all inputs based on the instruction of ComBat() function in 'sva' package. "Pheno_LMS" is my clinical data inclus batchId of my samples and "TCGA_expr_log" is my expression data. my code is below:
Pheno_LMS structure:
Sample        batchId   age  . . . 
GSM71019.CEL    396     63
GSM71020.CEL    396     58
GSM71021.CEL    410     85
GSM71020.CEL    411     58
GSM71021.CEL    410     40
.
.
.
dim(Pheno_LMS)
[1] 74 37

TCGA_expr_log structure:
                   GSM71019.CEL 1      GSM71019.CEL 2        GSM71019.CEL 3  . . . 
    Gene id          
  ENSG00000000003     10.115170           8.628044                  8.779235  
  ENSG00000000005     5.345168            5.063598                   5.113116
  ENSG00000000009     6.348024            6.663625                   6.465892     
    .
    .
    .
dim(TCGA_expr_log)
[1] 42817    74

batch = Pheno_LMS$batchId
modcombat = model.matrix(~1, data=Pheno_LMS)
TCGA_expr_Co <-  ComBat(as.matrix(TCGA_expr_log),batch = batch,mod = modcombat, par.prior = TRUE,mean.only = TRUE)

but when I run that function, I got below error:
Error in apply(dat[, batch == batch_level], 1, function(x) { : 
  dim(X) must have a positive length

Now, I need anybody comments about my problem. thanks
P.S: when i call batch I see:
> batch
 [1] 13 13 13 14 14 14 14 14 14 14 14 6  6  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7 
[29] 7  8  7  8  11 11 11 14 12 1  6  4  2  3  3  3  3  6  6  1  3  9  8  6  11 7  3  5 
[57] 4  5  5  4  4  11 8  8  12 12 12 12 12 10 10 10 11 13
Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14

**Tags:** r, batch-effects, combat, sva
> **Comments:**
> **username_1**: `dat[, batch == batch_level]` is likely empty.  
> **username_0**: Thanks for @DevonRyan comment. at the end of my post, I add "batch" content. As you see, when I call batch, for each sample I see a batch value. I have 14 batches.based on your knowledge how can I solve this problem?  
> **username_1**: You need to show us something that’s reproducible. As is we need to read your mind to figure out exactly what’s going wrong.  
> **username_0**: Dear @DevonRyan, for finding problem I save "Pheno_LMS" and "TCGA_expr_log" to "my_data.RData" which you can download it from this link:         https://www.dropbox.com/s/is4b9laou305d5k/my_data.RData?dl=0  
> **username_0**: Dear @DevonRyan, I appreciate it if you check my_data.RData and guide me for solving the problem.  

---

## 2 answers

### Answer by username_1 on 2019-12-03. Score: 1 | Accepted answer

`batch = Pheno_LMS$batchId` should be `batch = Pheno_LMS$BatchId`, if you fix that then at least the code you showed works just fine.

> **Comments:**
> **username_0**: Thanks @DevonRyan.In addition of your comment I found 2 batches by just 1 sample. So, I removed them from my TCGA_expr_log and now ComBat() runs perfectly.  

---

### Answer by username_2 on 2019-12-02. Score: 0

as suggested in the comment you should verify if the command:
dat[, batch == batch_level]

returns an empty object or not. Apparently, batch and batch_level do not have anyting that match and the function tries to run over an empty dataframe.

> **Comments:**
> **username_0**: Thanks for @gabt comment. in the end of my post, I add "batch" content. As you see, when I call batch, for each sample I see a batch value. i have 14 batches.  

---

