<p>Your question stated differently is as follows:</p>
<blockquote>
<p>I have data in long format and need to change it to wide format. For that, I want to keep the <code>gene_raw</code>, use <code>patient_id</code> for the column names and <code>gene_code</code> for the row names. How can I do this easily?</p>
</blockquote>
<p>Assuming you know how to read that into R as a data.frame that I'll call <code>d</code>:</p>
<pre><code>library(tidyr)
library(dplyr)
reformatted = d %&gt;% select(patient_id, gene_code, gene_raw) %&gt;% spread(patient_id, gene_raw) %&gt;% column_to_rownames(gene_code)
</code></pre>
<p>Or something along those lines.</p>

CC BY-SA 4.0
2020-11-06T08:56:02.087Z
2020-11-06T08:56:02.087Z
Devon Ryan
<p>I have an RNAseq gene expression file (Count data) as follows, I need to conduct a Differential gene expression analysis between Patients, for that, I need to have this file as a Matrix of Rows as Genes and columns as counts (samples), how best can I get this as matrix file?</p>
<p>Thanks a lot for your help, Stay safe</p>
<p><a href="https://i.sstatic.net/pOUdw.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/pOUdw.png" alt="My data looks like the following" /></a></p>

CC BY-SA 4.0
2020-11-06T17:50:42.513Z
Cross posted here:

https://www.biostars.org/p/471641/
swbarnes2
<rna-seq><gene-expression><format-conversion>
Gene expression Table to Expression Matrix converstion
# Gene expression Table to Expression Matrix converstion

(Asked by: username_0 on 2020-11-06)

I have an RNAseq gene expression file (Count data) as follows, I need to conduct a Differential gene expression analysis between Patients, for that, I need to have this file as a Matrix of Rows as Genes and columns as counts (samples), how best can I get this as matrix file?

Thanks a lot for your help, Stay safe

[![My data looks like the following](https://i.sstatic.net/pOUdw.png)](https://i.sstatic.net/pOUdw.png)

**Tags:** rna-seq, gene-expression, format-conversion
> **Comments:**
> **username_1**: Cross posted here:  https://www.biostars.org/p/471641/  

---

## 1 answers

### Answer by username_2 on 2020-11-06. Score: 1 | Accepted answer

Your question stated differently is as follows:

I have data in long format and need to change it to wide format. For that, I want to keep the gene_raw, use patient_id for the column names and gene_code for the row names. How can I do this easily?

Assuming you know how to read that into R as a data.frame that I'll call d:
library(tidyr)
library(dplyr)
reformatted = d %>% select(patient_id, gene_code, gene_raw) %>% spread(patient_id, gene_raw) %>% column_to_rownames(gene_code)

Or something along those lines.


---

