<p><code>read.csv</code> produces a data.frame. You'll need <code>rownames(a) &lt;- b$col_name</code>, not <code>rownames(a) &lt;- b</code>.</p>

CC BY-SA 4.0
2021-06-16T16:12:41.473Z
2021-06-16T16:12:41.473Z
Ram RS
<p>There are a couple of problems:</p>
<ul>
<li>As Ram RS said you get a data frame when you load that second CSV.  Since the information is stored in a row rather than a column you need a little more work to extract it too.</li>
<li>R will silently mangle your column names from the first data frame and will also interpret the second file as having column names (and then mangle those too) unless you give extra arguments to <code>read.csv</code> to prevent it.</li>
<li>The fact that you only have four identifiers (not 3114) in the second file means you can't use those as row names.  Your rows in <code>a</code> are cells, not patients, right?  You could make up something unique based on both patient and cell info but I'd suggest avoiding that.</li>
</ul>
<p>First you could use something like this (going straight from Dropbox here) to read the files without losing information:</p>
<pre><code>a &lt;- read.csv(&quot;http://dl.dropboxusercontent.com/s/slkhspz7lgdgph6/ascites.csv&quot;, check.names = FALSE)
b &lt;- read.csv(&quot;http://dl.dropboxusercontent.com/s/iq8lt7qrh7hdtq8/ascitesRownames.csv&quot;, header = FALSE)
</code></pre>
<p>Then, since you want to associate more information than can easily fit in a &quot;wide-format&quot; (matrix-like) data frame, and row and column names are so easily mangled by R, I'd suggest using a &quot;long-format&quot; data frame instead.  The package reshape2 is very helpful for this.  Here you can put the patient IDs as an additional column in your first data frame, explicitly label the cells, and then make a new data frame where each row is a cell and gene combination for a particular patient:</p>
<pre><code>a &lt;- cbind(patient = as.character(b[1, ]), cell_id = 1:nrow(a), a)
gene_counts &lt;- reshape2::melt(
  a, id.vars = 1:2, variable.name = &quot;gene&quot;, value.name = &quot;count&quot;)
</code></pre>
<p>So you get something like this:</p>
<pre><code>&gt; head(gene_counts)
  patient cell_id gene count
1   7873M       1 A1BG     1
2   7873M       2 A1BG     0
3   7873M       3 A1BG     2
4   7873M       4 A1BG     0
5   7873M       5 A1BG     1
6   7873M       6 A1BG     0
</code></pre>
<p>(Note the gene column is a factor there.)</p>
<p>I'd be very careful with row and column names since R is so prone to altering them without warning, and in general with assuming things will match up in the right order like the rows of the one table and the columns of the other.  Hadley Wickham has a good summary of the problems with row names in particular:</p>
<p><a href="https://adv-r.hadley.nz/vectors-chap.html#rownames" rel="nofollow noreferrer">https://adv-r.hadley.nz/vectors-chap.html#rownames</a></p>

CC BY-SA 4.0
2021-06-16T16:50:15.157Z
+1 agree with the comment about row names. I think data.table has the right idea in doing away with them completely and using keys instead.
user438383
CC BY-SA 4.0
2021-06-16T16:48:55.360Z
2021-06-16T16:48:55.360Z
Jesse
<p>I have two .csv files</p>
<p>One for raw read counts of each gene and the other 3114 single cells for four patients which should be the row names of the first file</p>
<p>As single cells per patients seems duplicated I am getting error and don't know how to read them together</p>
<p>Any way this is the files</p>
<p><a href="https://www.dropbox.com/s/slkhspz7lgdgph6/ascites.csv?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/slkhspz7lgdgph6/ascites.csv?dl=0</a></p>
<p><a href="https://www.dropbox.com/s/iq8lt7qrh7hdtq8/ascitesRownames.csv?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/iq8lt7qrh7hdtq8/ascitesRownames.csv?dl=0</a></p>
<p>I tried</p>
<pre><code>a=read.csv(&quot;ascites.csv&quot;)
Warning message:
In doTryCatch(return(expr), name, parentenv, handler) :
  restarting interrupted promise evaluation
b=read.csv(&quot;ascitesRownames.csv&quot;)
View(b)
View(a)
rownames(a)=b
</code></pre>
<blockquote>
<p>Error in <code>.rowNamesDF&lt;-</code>(x, value = value) :    duplicate 'row.names'
are not allowed In addition: Warning message: non-unique value when
setting 'row.names': ‘logical(0)’</p>
</blockquote>
<p>Any help in having a matrix in row names are 3114 single cells and genes in columns?</p>

CC BY-SA 4.0
2021-06-16T15:39:25.973Z
Can you please ``dput()`` the head of files so we don't have to download them from dropbox? Thanks.
user438383
<r><single-cell>
Unifying these files together
# Unifying these files together

(Asked by: username_0 on 2021-06-16)

I have two .csv files
One for raw read counts of each gene and the other 3114 single cells for four patients which should be the row names of the first file
As single cells per patients seems duplicated I am getting error and don't know how to read them together
Any way this is the files
https://www.dropbox.com/s/slkhspz7lgdgph6/ascites.csv?dl=0
https://www.dropbox.com/s/iq8lt7qrh7hdtq8/ascitesRownames.csv?dl=0
I tried
a=read.csv("ascites.csv")
Warning message:
In doTryCatch(return(expr), name, parentenv, handler) :
  restarting interrupted promise evaluation
b=read.csv("ascitesRownames.csv")
View(b)
View(a)
rownames(a)=b


Error in .rowNamesDF<-(x, value = value) :    duplicate 'row.names'
are not allowed In addition: Warning message: non-unique value when
setting 'row.names': ‘logical(0)’

Any help in having a matrix in row names are 3114 single cells and genes in columns?

**Tags:** r, single-cell
> **Comments:**
> **username_1**: Can you please ``dput()`` the head of files so we don't have to download them from dropbox? Thanks.  

---

## 2 answers

### Answer by username_3 on 2021-06-16. Score: 1

There are a couple of problems:

As Ram RS said you get a data frame when you load that second CSV.  Since the information is stored in a row rather than a column you need a little more work to extract it too.
R will silently mangle your column names from the first data frame and will also interpret the second file as having column names (and then mangle those too) unless you give extra arguments to read.csv to prevent it.
The fact that you only have four identifiers (not 3114) in the second file means you can't use those as row names.  Your rows in a are cells, not patients, right?  You could make up something unique based on both patient and cell info but I'd suggest avoiding that.

First you could use something like this (going straight from Dropbox here) to read the files without losing information:
a <- read.csv("http://dl.dropboxusercontent.com/s/slkhspz7lgdgph6/ascites.csv", check.names = FALSE)
b <- read.csv("http://dl.dropboxusercontent.com/s/iq8lt7qrh7hdtq8/ascitesRownames.csv", header = FALSE)

Then, since you want to associate more information than can easily fit in a "wide-format" (matrix-like) data frame, and row and column names are so easily mangled by R, I'd suggest using a "long-format" data frame instead.  The package reshape2 is very helpful for this.  Here you can put the patient IDs as an additional column in your first data frame, explicitly label the cells, and then make a new data frame where each row is a cell and gene combination for a particular patient:
a <- cbind(patient = as.character(b[1, ]), cell_id = 1:nrow(a), a)
gene_counts <- reshape2::melt(
  a, id.vars = 1:2, variable.name = "gene", value.name = "count")

So you get something like this:
> head(gene_counts)
  patient cell_id gene count
1   7873M       1 A1BG     1
2   7873M       2 A1BG     0
3   7873M       3 A1BG     2
4   7873M       4 A1BG     0
5   7873M       5 A1BG     1
6   7873M       6 A1BG     0

(Note the gene column is a factor there.)
I'd be very careful with row and column names since R is so prone to altering them without warning, and in general with assuming things will match up in the right order like the rows of the one table and the columns of the other.  Hadley Wickham has a good summary of the problems with row names in particular:
https://adv-r.hadley.nz/vectors-chap.html#rownames

> **Comments:**
> **username_1**: +1 agree with the comment about row names. I think data.table has the right idea in doing away with them completely and using keys instead.  

---

### Answer by username_2 on 2021-06-16. Score: 0

`read.csv` produces a data.frame. You'll need `rownames(a) <- b$col_name`, not `rownames(a) <- b`.


---

