text: user6672: I am new here so forgive me if I don't provide all the details in the first go. There is a very similar question (how to solve “dim(X) must have a positive length” at running ComBat function in R) but the solution did not work for me.
  I am getting Error in apply(dat[, batch == batch_level], 1, function(x) { :    dim(X) must have a positive length when I use the sva library's comBat() function.
  It works when I use the following:
  > dim(log_proteins_norm_sorted)
  [1] 5415   66
  
  > log_proteins_norm_sorted[1:5,1:5]
        1027     1075     1116     1119    11211
  1 22.50513 20.64312 21.56182 22.01455 23.17497
  2 21.67946 20.90994 21.61928 21.92025 21.98503
  3 21.06812 20.52647 20.72076 20.79732 21.38957
  4 18.95485 19.70027 18.39504 20.20218 19.79887
  5 22.75966 22.32306 22.32936 22.39271 22.84204
  
  > dim(samples_pheno)
  [1] 66 24
  
  > samples_pheno[1:3,1:3]
       Trial Number  Trial Sample
  1027      222       A   1027
  1075      223       A   1075
  1116      224       A   1116
  
  > proteins_combat = ComBat(dat= as.matrix(log_proteins_norm_sorted), batch=samples_pheno$Batch ,  par.prior = T)
  Found10batches
  Adjusting for0covariate(s) or covariate level(s)
  Standardizing Data across genes
  Fitting L/S model and finding priors
  Finding parametric adjustments
  Adjusting the Data
  
  So as you can see, this works. However, when I subset some of the samples and reduce the sample number from 66 to 55 and run comBat, I get the following:
  > dim(log_proteins_MRD_sorted)
  [1] 5415   55
  > log_proteins_MRD_sorted[1:5,1:5]
        1027     1075     1116     1119     1217
  1 22.55492 20.69292 21.61162 22.06435 21.86900
  2 21.72926 20.95974 21.66907 21.97005 21.35203
  3 21.11791 20.57627 20.77055 20.84712 21.01896
  4 19.00465 19.75006 18.44483 20.25198 18.95107
  5 22.80946 22.37285 22.37915 22.44250 22.50524
  
  > dim(samples_pheno3)
  [1] 55 24
  > samples_pheno3[1:3,1:3]
       Trial Number  Trial Sample
  1027      222        A   1027
  1075      223        A   1075
  1116      224        A   1116
  
  > proteins_combat_MRD = ComBat(dat= as.matrix(log_proteins_MRD_sorted), batch=samples_pheno3$Batch ,  par.prior = T)
  Error in apply(dat[, batch == batch_level], 1, function(x) { : 
    dim(X) must have a positive length
  
  In case you wanted to know, I am working with the same original file for log_proteins_MRD_sorted and log_proteins_norm_sorted. I just removed the 11 samples from the 66 samples and then normalised the data again (hence slight difference in values between the two objects. 
  For samples_pheno3, I run this code:
  > #Subset the phenotype data to those samples with only MRD data
  > samples_pheno2 <- samples_pheno[-c(5,6,11,16,21, 26, 29,30,32, 45,56),]
  > MRD_NA1 <- rownames(samples_pheno2)[!is.na(samples_pheno2$MRD)]
  > samples_pheno3 <- samples_pheno2[MRD_NA1,]
  
  Also this is out my data looks like for the batches:
  > samples_pheno3$Batch
   [1] 7  5  6  9  7  7  7  7  8  8  8  8  9  9  9  9  10 10 10 10 11 11 11 2  7  2  2  2  9  3  3  3  3  3  3  3  7 
  [38] 10 3  4  10 4  4  8  4  4  4  5  5  5  8  11 5  5  5 
  Levels: 2 3 4 5 6 7 8 9 10 11
  
  So just by subsetting the data to 55 samples, it doesn't seem to work for some reason. Is it the way I am subsetting the data which is confusing the code?
  I would be grateful for your advice. 
  
  user6672: With the help of my very capable local bioinformatician, I have found a solution. Essentially, the problem was that when I reduced the sample size from 66 to 55, there was a batch number which only had 1 sample in it. When I removed that sample from the analysis, and ensured that each batch had at least 2 samples in it, the ComBat() then worked nicely!
  Hope this helps future googlers.
  
original_text: 6672: <p>I am new here so forgive me if I don't provide all the details in the first go. There is a very similar question (<a href="https://bioinformatics.stackexchange.com/questions/10914/how-to-solve-dimx-must-have-a-positive-length-at-running-combat-function-in">how to solve “dim(X) must have a positive length” at running ComBat function in R</a>) but the solution did not work for me.</p>
  
  <p>I am getting <code>Error in apply(dat[, batch == batch_level], 1, function(x) { :    dim(X) must have a positive length</code> when I use the <code>sva</code> library's <code>comBat()</code> function.</p>
  
  <p>It works when I use the following:</p>
  
  <pre><code>&gt; dim(log_proteins_norm_sorted)
  [1] 5415   66
  
  &gt; log_proteins_norm_sorted[1:5,1:5]
        1027     1075     1116     1119    11211
  1 22.50513 20.64312 21.56182 22.01455 23.17497
  2 21.67946 20.90994 21.61928 21.92025 21.98503
  3 21.06812 20.52647 20.72076 20.79732 21.38957
  4 18.95485 19.70027 18.39504 20.20218 19.79887
  5 22.75966 22.32306 22.32936 22.39271 22.84204
  
  &gt; dim(samples_pheno)
  [1] 66 24
  
  &gt; samples_pheno[1:3,1:3]
       Trial Number  Trial Sample
  1027      222       A   1027
  1075      223       A   1075
  1116      224       A   1116
  
  &gt; proteins_combat = ComBat(dat= as.matrix(log_proteins_norm_sorted), batch=samples_pheno$Batch ,  par.prior = T)
  Found10batches
  Adjusting for0covariate(s) or covariate level(s)
  Standardizing Data across genes
  Fitting L/S model and finding priors
  Finding parametric adjustments
  Adjusting the Data
  </code></pre>
  
  <p>So as you can see, this works. However, when I subset some of the samples and reduce the sample number from 66 to 55 and run comBat, I get the following:</p>
  
  <pre><code>&gt; dim(log_proteins_MRD_sorted)
  [1] 5415   55
  &gt; log_proteins_MRD_sorted[1:5,1:5]
        1027     1075     1116     1119     1217
  1 22.55492 20.69292 21.61162 22.06435 21.86900
  2 21.72926 20.95974 21.66907 21.97005 21.35203
  3 21.11791 20.57627 20.77055 20.84712 21.01896
  4 19.00465 19.75006 18.44483 20.25198 18.95107
  5 22.80946 22.37285 22.37915 22.44250 22.50524
  
  &gt; dim(samples_pheno3)
  [1] 55 24
  &gt; samples_pheno3[1:3,1:3]
       Trial Number  Trial Sample
  1027      222        A   1027
  1075      223        A   1075
  1116      224        A   1116
  
  &gt; proteins_combat_MRD = ComBat(dat= as.matrix(log_proteins_MRD_sorted), batch=samples_pheno3$Batch ,  par.prior = T)
  Error in apply(dat[, batch == batch_level], 1, function(x) { : 
    dim(X) must have a positive length
  </code></pre>
  
  <p>In case you wanted to know, I am working with the same original file for <code>log_proteins_MRD_sorted</code> and <code>log_proteins_norm_sorted</code>. I just removed the 11 samples from the 66 samples and then normalised the data again (hence slight difference in values between the two objects. </p>
  
  <p>For <code>samples_pheno3</code>, I run this code:</p>
  
  <pre><code>&gt; #Subset the phenotype data to those samples with only MRD data
  &gt; samples_pheno2 &lt;- samples_pheno[-c(5,6,11,16,21, 26, 29,30,32, 45,56),]
  &gt; MRD_NA1 &lt;- rownames(samples_pheno2)[!is.na(samples_pheno2$MRD)]
  &gt; samples_pheno3 &lt;- samples_pheno2[MRD_NA1,]
  </code></pre>
  
  <p>Also this is out my data looks like for the batches:</p>
  
  <pre><code>&gt; samples_pheno3$Batch
   [1] 7  5  6  9  7  7  7  7  8  8  8  8  9  9  9  9  10 10 10 10 11 11 11 2  7  2  2  2  9  3  3  3  3  3  3  3  7 
  [38] 10 3  4  10 4  4  8  4  4  4  5  5  5  8  11 5  5  5 
  Levels: 2 3 4 5 6 7 8 9 10 11
  </code></pre>
  
  <p>So just by subsetting the data to 55 samples, it doesn't seem to work for some reason. Is it the way I am subsetting the data which is confusing the code?</p>
  
  <p>I would be grateful for your advice. </p>
   6672: <p>With the help of my very capable local bioinformatician, I have found a solution. Essentially, the problem was that when I reduced the sample size from 66 to 55, there was a batch number which only had 1 sample in it. When I removed that sample from the analysis, and ensured that each batch had at least 2 samples in it, the <code>ComBat()</code> then worked nicely!</p>
  
  <p>Hope this helps future googlers.</p>
  