<p>I believe that </p>

<pre><code>uniqsampleID = 1:length(unique(mutations<span class="math-container">$sampleID))
names(uniqsampleID) = unique(mutations$</span>sampleID)
mutations<span class="math-container">$sampleID = uniqsampleID[mutations$</span>sampleID]
</code></pre>

<p>should work, assuming <code>mutations</code> is a data frame.</p>

<hr>

<p>Explanation:</p>

<p><code>1:N</code> creates a vector of length N, containing 1, 2, ..., N. So using <code>1:length(X)</code> will do this for each item in X, so <code>1:length(unique(mutations$sampleID))</code> should give you a vector of <code>1:139</code>.</p>

<p>Then <code>names(uniqsampleID) = unique(mutations$sampleID)</code> assigns to each of these numbers one of these sample IDs, which allows you to retrieve the sample number with <code>uniqsampleID[mutations$sampleID]</code>.</p>

CC BY-SA 4.0
2019-11-07T14:00:03.513Z
Sorry that gives 3574407 sample IDs while I only have 139 samples for each I have 25 chromosome and a lot of mutations; I mean sampleID must contains maximum 139 unique numbers
Zizogolu
CC BY-SA 4.0
2019-11-07T14:02:16.380Z
Ah I see! I'll change it.
Whitehot
CC BY-SA 4.0
2019-11-07T14:17:35.130Z
Yessssss worked thanks; Sorry in chr column I have the number of chromosome 1:23 plus to X and Y; How I can add chr prefix to this column? I mean instead of 1 I should have chr1
Zizogolu
CC BY-SA 4.0
2019-11-07T14:24:19.700Z
Technically should be another question, but `paste0()` can handle that. It would probably be `mutations$chr = paste0("chr", mutations$chr)`
Whitehot
CC BY-SA 4.0
2019-11-07T13:51:12.747Z
2019-11-07T14:06:16.403Z
2019-11-07T14:06:16.403Z
Whitehot
<p>I have a .txt file; I have 139 unique sample IDs in sampleID column</p>

<pre><code>&gt; head(mutations)
                                                                              sampleID chr     pos ref mut
4 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1  920552   G   A
5 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 1350208   G   A
6 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 1447367   T   G
7 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 1909310   G   A
8 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 2172675   G   C
9 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 2846623   C   T
&gt; 

&gt; dim(mutations)
[1] 3574407       5
&gt; 
</code></pre>

<p>I want to change name of sample from 1 to 139 in R or Linux terminal but I don't know how to do that, any help please?</p>

<p>Thanks </p>

<r><linux><looper>
Replacing some strings in R or terminal
# Replacing some strings in R or terminal

(Asked by: username_0 on 2019-11-07)

I have a .txt file; I have 139 unique sample IDs in sampleID column
> head(mutations)
                                                                              sampleID chr     pos ref mut
4 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1  920552   G   A
5 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 1350208   G   A
6 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 1447367   T   G
7 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 1909310   G   A
8 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 2172675   G   C
9 LP2000107-DNA_A01_vs_LP2000102-DNA_A01__pv.1.6__rg.grch37_g1k__al.bwa_mem__.snp.pass   1 2846623   C   T
> 

> dim(mutations)
[1] 3574407       5
> 

I want to change name of sample from 1 to 139 in R or Linux terminal but I don't know how to do that, any help please?
Thanks

**Tags:** r, linux, looper

---

## 1 answers

### Answer by username_1 on 2019-11-07. Score: 1 | Accepted answer

I believe that 
uniqsampleID = 1:length(unique(mutations$sampleID))
names(uniqsampleID) = unique(mutations$sampleID)
mutations$sampleID = uniqsampleID[mutations$sampleID]

should work, assuming mutations is a data frame.

Explanation:
1:N creates a vector of length N, containing 1, 2, ..., N. So using 1:length(X) will do this for each item in X, so 1:length(unique(mutations$sampleID)) should give you a vector of 1:139.
Then names(uniqsampleID) = unique(mutations$sampleID) assigns to each of these numbers one of these sample IDs, which allows you to retrieve the sample number with uniqsampleID[mutations$sampleID].

> **Comments:**
> **username_0**: Sorry that gives 3574407 sample IDs while I only have 139 samples for each I have 25 chromosome and a lot of mutations; I mean sampleID must contains maximum 139 unique numbers  
> **username_1**: Ah I see! I'll change it.  
> **username_0**: Yessssss worked thanks; Sorry in chr column I have the number of chromosome 1:23 plus to X and Y; How I can add chr prefix to this column? I mean instead of 1 I should have chr1  
> **username_1**: Technically should be another question, but `paste0()` can handle that. It would probably be `mutations$chr = paste0("chr", mutations$chr)`  

---

