<p><em>Answer from @maximilian-press, converted from comment:</em></p>
<p>Sometimes casting to <code>as.numeric()</code> will convert from two-dimensional matrix to one-dimensional vector. Based on the output you put into comments (which would be helpful in the question), I wonder if this has occurred and caused an issue. For <code>data.matrix</code>, I suggest reading docs for that function- it should have returned only numeric, such that your <code>med</code> variable is likely not numeric, or there is a problem with your data. Possibly the tidyverse <code>tibble</code> created by <code>read_excel</code> might be the issue. I don't know enough tidyverse to say what exactly is going on.</p>

CC BY-SA 4.0
2021-09-29T04:36:00.260Z
2021-09-29T04:36:00.260Z
gringer
<p>I'm trying to run the Non-Negative Matrix Factorization(NMF) for my Gene Expression dataset which was originally in matrix form. But it throws errors as given below:</p>
<pre><code>expr &lt;- read_excel(&quot;./expr.xlsx&quot;)
expr&lt;- as.matrix(expr)

#removing the first column containing gene symbols before clustering
expr_rm &lt;- expr[,-1]
class(expr_rm)
[1] &quot;matrix&quot; &quot;array&quot; 
</code></pre>
<p>So converted these expr_rm to numeric:</p>
<pre><code>expr_rm &lt;- as.numeric(expr_rm)

#removing negative values for running NMF
eps &lt;- .Machine$double.eps
expr_rm[expr_rm&lt;=0] = eps

estimate &lt;- NMF::nmf(expr_rm, 3, nrun = 10, method = &quot;lee&quot;, seed = 123)
</code></pre>
<blockquote>
<p>Error: NMF::nmf - 10/10 fit(s) threw an error.
Error(s) thrown:
run #1: 'x' must be numeric
Timing stopped at: 1.95 0.14 4.4*</p>
</blockquote>
<pre><code>cluster &lt;- cbind(names(NMF::predict(estimate)), NMF::predict(estimate))
</code></pre>
<blockquote>
<p>Error in h(simpleError(msg, call)) :
error in evaluating the argument 'object' in selecting a method for function 'predict': object 'estimate' not found</p>
</blockquote>
<pre><code>result &lt;- list(estimate, cluster)
</code></pre>
<blockquote>
<p>Error: object 'estimate' not found</p>
</blockquote>
<p><strong>For clarity, my expr_rm has row indices as 1, 2, 3, 4,.. which indicates different genes and column headings as samples.</strong></p>
<p>Please suggest a solution for this soon!</p>
<p>Additional information (requested from comments):</p>
<pre><code>&gt;summary(expr_rm) - before conversion 
 Sample_C44_0001.genes.results Sample_C44_0002.genes.results Sample_C44_0003.genes.results Sample_C44_0005.genes.results 
 Length:145   Length:145 Length:145  Length:145  Class :character

 &gt; summary(expr_rm) - after numeric type conversion
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.000   1.808   4.264   4.220   6.333  16.246
</code></pre>
<p>If in expr_rm is data.matrix() also, it gives the error like:</p>
<pre><code>&gt; median_centered_data&lt;- expr_rm - med
Error in expr_rm - med : non-numeric argument to binary operator 
</code></pre>

<r><genomics><gene-expression><clustering><modelling>
NMF clustering not giving results
# NMF clustering not giving results

(Asked by: username_0 on 2021-09-24)

I'm trying to run the Non-Negative Matrix Factorization(NMF) for my Gene Expression dataset which was originally in matrix form. But it throws errors as given below:
expr <- read_excel("./expr.xlsx")
expr<- as.matrix(expr)

#removing the first column containing gene symbols before clustering
expr_rm <- expr[,-1]
class(expr_rm)
[1] "matrix" "array" 

So converted these expr_rm to numeric:
expr_rm <- as.numeric(expr_rm)

#removing negative values for running NMF
eps <- .Machine$double.eps
expr_rm[expr_rm<=0] = eps

estimate <- NMF::nmf(expr_rm, 3, nrun = 10, method = "lee", seed = 123)


Error: NMF::nmf - 10/10 fit(s) threw an error.
Error(s) thrown:
run #1: 'x' must be numeric
Timing stopped at: 1.95 0.14 4.4*

cluster <- cbind(names(NMF::predict(estimate)), NMF::predict(estimate))


Error in h(simpleError(msg, call)) :
error in evaluating the argument 'object' in selecting a method for function 'predict': object 'estimate' not found

result <- list(estimate, cluster)


Error: object 'estimate' not found

For clarity, my expr_rm has row indices as 1, 2, 3, 4,.. which indicates different genes and column headings as samples.
Please suggest a solution for this soon!
Additional information (requested from comments):
>summary(expr_rm) - before conversion 
 Sample_C44_0001.genes.results Sample_C44_0002.genes.results Sample_C44_0003.genes.results Sample_C44_0005.genes.results 
 Length:145   Length:145 Length:145  Length:145  Class :character

 > summary(expr_rm) - after numeric type conversion
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.000   1.808   4.264   4.220   6.333  16.246

If in expr_rm is data.matrix() also, it gives the error like:
> median_centered_data<- expr_rm - med
Error in expr_rm - med : non-numeric argument to binary operator

**Tags:** r, genomics, gene-expression, clustering, modelling

---

## 1 answers

### Answer by username_1 on 2021-09-29. Score: 0 | Accepted answer

*Answer from @maximilian-press, converted from comment:*

Sometimes casting to `as.numeric()` will convert from two-dimensional matrix to one-dimensional vector. Based on the output you put into comments (which would be helpful in the question), I wonder if this has occurred and caused an issue. For `data.matrix`, I suggest reading docs for that function- it should have returned only numeric, such that your `med` variable is likely not numeric, or there is a problem with your data. Possibly the tidyverse `tibble` created by `read_excel` might be the issue. I don't know enough tidyverse to say what exactly is going on.


---

