Answers.Body: <p>If your data is in a dataframe you can make use of <code>plyr</code>. Using <code>mapvalues()</code> from this library, you can rename provide a list of keys and a list of values. Instead of explicitly listing the keys, we can just use <code>unique(mat$Tumor_Sample_Barcode)</code> [credit to @dc37]. I use the <code>paste0</code> function here and the colon operator to generate the vector of <code>A1,A2,...,A21</code>. </p>
  
  <p>Assign the vector produced by <code>mapvalues()</code> to the column and you're off!</p>
  
  <pre><code>install.packages("plyr")
  mat$Tumor_Sample_Barcode &lt;-
  plyr::mapvalues(x=mat$Tumor_Sample_Barcode,
                  from=unique(mat$Tumor_Sample_Barcode),
                  to=paste0("A", 1:21))
  </code></pre>
  
  <p>I'd recommend keeping this column and adding a new column with your simplified sample names, but this is not what you requested.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-09T19:06:25.937Z
Answers.Text: Nice solution, i'm not that familiar with `plyr`. Just a quick question, can you replace the current part of `from =c(... ` by `from = levels(mat$Tumor_Sample_Barcode)` ?
Answers.UserDisplayName: dc37
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-09T19:10:12.660Z
Answers.Text: Nice improvement! `levels()` will only work if the vector is a factor, so a more universal solution would be to replace it with `from=unique(mat$Tumor_Sample_Barcode)`. I've update my answer to include this.
Answers.UserDisplayName: Kohl Kinning
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-09T19:50:10.130Z
Answers.Text: Good to know ;) Thanks for sharing this solution using `plyr`
Answers.UserDisplayName: dc37
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-08T10:52:33.510Z
Answers.Text: No need for extra packages (mapvalues, aka forloop), see my answer.
Answers.UserDisplayName: zx8754
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-09T18:53:56.640Z
Answers.LastActivityDate: 2019-12-10T20:54:46.133Z
Answers.LastEditDate: 2019-12-10T20:54:46.133Z
Answers.UserDisplayName: Kohl Kinning
Answers.Body: <p>Change to <em>factor</em> then to <em>numeric</em>:</p>
  
  <pre><code>mat<span class="math-container">$Tumor_Sample_Barcode &lt;- as.numeric(as.factor(mat$</span>Tumor_Sample_Barcode))
  </code></pre>
  
  <p>Using @dc37 example data:</p>
  
  <pre><code>as.numeric(as.factor(df$vect))
  # [1] 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
  </code></pre>
  
  <p>If we need to prefix with "A"</p>
  
  <pre><code>paste0("A", as.numeric(as.factor(df$vect)))
  # [1] "A1" "A2" "A3" "A4" "A1" "A2" "A3" "A4" "A1" "A2" "A3" "A4" "A1" "A2" "A3" "A4" "A1" "A2" "A3" "A4"
  </code></pre>
  
  <p>Or we can keep them as <em>factors</em>:</p>
  
  <pre><code>x &lt;- sort(unique(df<span class="math-container">$vect))
  factor(df$</span>vect, levels = x, labels = paste0("A", seq_along(x)))
  # [1] A1 A2 A3 A4 A1 A2 A3 A4 A1 A2 A3 A4 A1 A2 A3 A4 A1 A2 A3 A4
  # Levels: A1 A2 A3 A4
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-08T10:50:10.843Z
Answers.LastActivityDate: 2020-01-08T10:56:49.523Z
Answers.LastEditDate: 2020-01-08T10:56:49.523Z
Answers.UserDisplayName: zx8754
Answers.Body: <p>Using <code>sub</code> in <code>base</code> R, you can:</p>
  
  <p>1) define the replacement vector:</p>
  
  <pre class="lang-r prettyprint-override"><code>vector = paste0("A",seq(1:21))
  </code></pre>
  
  <p>2) and loop over each levels of your first columns to replace them individually:</p>
  
  <pre class="lang-r prettyprint-override"><code>for(i in 1:length(levels(mat<span class="math-container">$Tumor_Sample_Barcode)))
  {
    levels(mat$Tumor_Sample_Barcode)[i] = sub(levels(mat$Tumor_Sample_Barcode)[i],vector[i], levels(mat$</span>Tumor_Sample_Barcode)[i])
  }
  </code></pre>
  
  <p><strong>Example:</strong></p>
  
  <p>Here, a small example to show you:</p>
  
  <pre class="lang-r prettyprint-override"><code>df = data.frame(vect = factor(rep(c("a","b","c","d"),5)),
                         value = rnorm(20))
  
  &gt; head(df)       
    vect        value
  1    a  0.418186287
  2    b -0.232936122
  3    c  1.563349091
  4    d -0.001453912
  5    a -0.453995163
  6    b -0.481360070
  </code></pre>
  
  <pre class="lang-r prettyprint-override"><code>vector = paste0("A",seq(1,4))
  for(i in 1:length(levels(df$vect)))
  {
    levels(df$vect)[i] = sub(levels(df$vect)[i],vector[i], levels(df$vect)[i]) 
  }
  </code></pre>
  
  <p>And you get:</p>
  
  <pre class="lang-r prettyprint-override"><code>&gt; head(df)
    vect        value
  1   A1  0.418186287
  2   A2 -0.232936122
  3   A3  1.563349091
  4   A4 -0.001453912
  5   A1 -0.453995163
  6   A2 -0.481360070
  </code></pre>
  
  <p>Does it answer your question ?</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-08T10:51:53.497Z
Answers.Text: No need for forloops, see my answer.
Answers.UserDisplayName: zx8754
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-09T18:30:44.147Z
Answers.LastActivityDate: 2019-12-09T19:50:38.223Z
Answers.LastEditDate: 2019-12-09T19:50:38.223Z
Answers.UserDisplayName: dc37
Body: <p>I have a very big matrix like</p>
  
  <pre><code>&gt; head(mat[1:4,1:4])
                       Tumor_Sample_Barcode Chromosome Start_Position End_Position
  1: LP6005334-DNA_H01_vs_LP6005333-DNA_H01       chr1        1026918      1026918
  2: LP6005334-DNA_H01_vs_LP6005333-DNA_H01       chr1       15772155     15772155
  3: LP6005334-DNA_H01_vs_LP6005333-DNA_H01       chr1       16054813     16054813
  4: LP6005334-DNA_H01_vs_LP6005333-DNA_H01       chr1       24120222     24120222
  
  &gt; dim(mat)
  [1] 450754     53
  &gt;
  </code></pre>
  
  <p>In <code>Tumor_Sample_Barcode</code> column I have the name of samples so that</p>
  
  <pre><code>&gt; unique(mat$Tumor_Sample_Barcode)
   [1] "LP6005334-DNA_H01_vs_LP6005333-DNA_H01"     "LP6005500-DNA_D03_vs_LP6005499-DNA_D03"    
   [3] "LP6005935-DNA_G02_vs_LP6005979-DNA_C01"     "LP6007600_vs_LP6007599"                    
   [5] "LP6008202-DNA_B03_vs_LP6008201-DNA_B03"     "LP6008269-DNA_F06_vs_LP6008268-DNA_G06"    
   [7] "LP6008334-DNA_A03_vs_LP6008335-DNA_E02"     "LP6008334-DNA_A04_vs_LP6008335-DNA_E03"    
   [9] "LP6008334-DNA_B02_vs_LP6008335-DNA_F01"     "LP6008334-DNA_C02_vs_LP6008335-DNA_G01"    
  [11] "LP6008334-DNA_D02_vs_LP6008335-DNA_H01"     "LP6008336-DNA_F02_vs_LP6008333-DNA_F02"    
  [13] "LP6008336-DNA_G01_vs_LP6008333-DNA_G01"     "LP6008336-DNA_H01_vs_LP6008333-DNA_H01"    
  [15] "LP6008336-DNA_H02_vs_LP6008333-DNA_H02"     "LP6008337-DNA_A07_vs_LP6008338-DNA_E06"    
  [17] "LP6008337-DNA_H06_vs_LP6008338-DNA_D06"     "LP6008460-DNA_A04_vs_LP6008340-DNA_D05__pv"
  [19] "LP6008460-DNA_D01_vs_LP6008340-DNA_E02__pv" "LP6008460-DNA_F02_vs_LP6008340-DNA_E05"    
  [21] "LP6008460-DNA_G03_vs_LP6008340-DNA_C05__pv"
  &gt;
  </code></pre>
  
  <p>I want to change the name of these samples to A1... A21 but I don't know how</p>
  
  <p>Can you help?</p>
  
  <p>Thank you</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-12-09T18:47:30.687Z
Comments.Text: Columns cannot be accessed from matrices using the dollar sign scheme e.g. `df$colname`. It's likely your data is a dataframe rather than a matrix. You can check with `str(mat)`.
Comments.UserDisplayName: Kohl Kinning
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-08T11:08:19.063Z
Comments.Text: Related post https://bioinformatics.stackexchange.com/q/10960/131
Comments.UserDisplayName: zx8754
Tags: <r>
Title: Changing the name of elements in a column
ThreadText: # Changing the name of elements in a column
  
  (Asked by: username_0 on 2019-12-09)
  
  I have a very big matrix like
  
  ```
  > head(mat[1:4,1:4])
                       Tumor_Sample_Barcode Chromosome Start_Position End_Position
  1: LP6005334-DNA_H01_vs_LP6005333-DNA_H01       chr1        1026918      1026918
  2: LP6005334-DNA_H01_vs_LP6005333-DNA_H01       chr1       15772155     15772155
  3: LP6005334-DNA_H01_vs_LP6005333-DNA_H01       chr1       16054813     16054813
  4: LP6005334-DNA_H01_vs_LP6005333-DNA_H01       chr1       24120222     24120222
  
  > dim(mat)
  [1] 450754     53
  >
  
  ```
  
  In `Tumor_Sample_Barcode` column I have the name of samples so that
  
  ```
  > unique(mat$Tumor_Sample_Barcode)
   [1] "LP6005334-DNA_H01_vs_LP6005333-DNA_H01"     "LP6005500-DNA_D03_vs_LP6005499-DNA_D03"    
   [3] "LP6005935-DNA_G02_vs_LP6005979-DNA_C01"     "LP6007600_vs_LP6007599"                    
   [5] "LP6008202-DNA_B03_vs_LP6008201-DNA_B03"     "LP6008269-DNA_F06_vs_LP6008268-DNA_G06"    
   [7] "LP6008334-DNA_A03_vs_LP6008335-DNA_E02"     "LP6008334-DNA_A04_vs_LP6008335-DNA_E03"    
   [9] "LP6008334-DNA_B02_vs_LP6008335-DNA_F01"     "LP6008334-DNA_C02_vs_LP6008335-DNA_G01"    
  [11] "LP6008334-DNA_D02_vs_LP6008335-DNA_H01"     "LP6008336-DNA_F02_vs_LP6008333-DNA_F02"    
  [13] "LP6008336-DNA_G01_vs_LP6008333-DNA_G01"     "LP6008336-DNA_H01_vs_LP6008333-DNA_H01"    
  [15] "LP6008336-DNA_H02_vs_LP6008333-DNA_H02"     "LP6008337-DNA_A07_vs_LP6008338-DNA_E06"    
  [17] "LP6008337-DNA_H06_vs_LP6008338-DNA_D06"     "LP6008460-DNA_A04_vs_LP6008340-DNA_D05__pv"
  [19] "LP6008460-DNA_D01_vs_LP6008340-DNA_E02__pv" "LP6008460-DNA_F02_vs_LP6008340-DNA_E05"    
  [21] "LP6008460-DNA_G03_vs_LP6008340-DNA_C05__pv"
  >
  
  ```
  
  I want to change the name of these samples to A1... A21 but I don't know how
  
  Can you help?
  
  Thank you
  
  **Tags:** r
  
  > **Comments:**
  > **username_1**: Columns cannot be accessed from matrices using the dollar sign scheme e.g. `df$colname`. It's likely your data is a dataframe rather than a matrix. You can check with `str(mat)`.  
  > **username_2**: Related post https://bioinformatics.stackexchange.com/q/10960/131  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2019-12-09. Score: 3 | Accepted answer
  
  If your data is in a dataframe you can make use of `plyr`. Using `mapvalues()` from this library, you can rename provide a list of keys and a list of values. Instead of explicitly listing the keys, we can just use `unique(mat$Tumor_Sample_Barcode)` [credit to @username_3]. I use the `paste0` function here and the colon operator to generate the vector of `A1,A2,...,A21`.
  
  Assign the vector produced by `mapvalues()` to the column and you're off!
  
  ```
  install.packages("plyr")
  mat$Tumor_Sample_Barcode <-
  plyr::mapvalues(x=mat$Tumor_Sample_Barcode,
                  from=unique(mat$Tumor_Sample_Barcode),
                  to=paste0("A", 1:21))
  
  ```
  
  I'd recommend keeping this column and adding a new column with your simplified sample names, but this is not what you requested.
  
  > **Comments:**
  > **username_3**: Nice solution, i'm not that familiar with `plyr`. Just a quick question, can you replace the current part of `from =c(... ` by `from = levels(mat$Tumor_Sample_Barcode)` ?  
  > **username_1**: Nice improvement! `levels()` will only work if the vector is a factor, so a more universal solution would be to replace it with `from=unique(mat$Tumor_Sample_Barcode)`. I've update my answer to include this.  
  > **username_3**: Good to know ;) Thanks for sharing this solution using `plyr`  
  > **username_2**: No need for extra packages (mapvalues, aka forloop), see my answer.  
  
  ---
  
  ### Answer by username_2 on 2020-01-08. Score: 2
  
  Change to *factor* then to *numeric*:
  
  ```
  mat$Tumor_Sample_Barcode <- as.numeric(as.factor(mat$Tumor_Sample_Barcode))
  
  ```
  
  Using @username_3 example data:
  
  ```
  as.numeric(as.factor(df$vect))
  # [1] 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
  
  ```
  
  If we need to prefix with "A"
  
  ```
  paste0("A", as.numeric(as.factor(df$vect)))
  # [1] "A1" "A2" "A3" "A4" "A1" "A2" "A3" "A4" "A1" "A2" "A3" "A4" "A1" "A2" "A3" "A4" "A1" "A2" "A3" "A4"
  
  ```
  
  Or we can keep them as *factors*:
  
  ```
  x <- sort(unique(df$vect))
  factor(df$vect, levels = x, labels = paste0("A", seq_along(x)))
  # [1] A1 A2 A3 A4 A1 A2 A3 A4 A1 A2 A3 A4 A1 A2 A3 A4 A1 A2 A3 A4
  # Levels: A1 A2 A3 A4
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2019-12-09. Score: 1
  
  Using `sub` in `base` R, you can:
  
  1) define the replacement vector:
  
  ```r
  vector = paste0("A",seq(1:21))
  
  ```
  
  2) and loop over each levels of your first columns to replace them individually:
  
  ```r
  for(i in 1:length(levels(mat$Tumor_Sample_Barcode)))
  {
    levels(mat$Tumor_Sample_Barcode)[i] = sub(levels(mat$Tumor_Sample_Barcode)[i],vector[i], levels(mat$Tumor_Sample_Barcode)[i])
  }
  
  ```
  
  **Example:**
  
  Here, a small example to show you:
  
  ```r
  df = data.frame(vect = factor(rep(c("a","b","c","d"),5)),
                         value = rnorm(20))
  
  > head(df)       
    vect        value
  1    a  0.418186287
  2    b -0.232936122
  3    c  1.563349091
  4    d -0.001453912
  5    a -0.453995163
  6    b -0.481360070
  
  ```
  
  ```r
  vector = paste0("A",seq(1,4))
  for(i in 1:length(levels(df$vect)))
  {
    levels(df$vect)[i] = sub(levels(df$vect)[i],vector[i], levels(df$vect)[i]) 
  }
  
  ```
  
  And you get:
  
  ```r
  > head(df)
    vect        value
  1   A1  0.418186287
  2   A2 -0.232936122
  3   A3  1.563349091
  4   A4 -0.001453912
  5   A1 -0.453995163
  6   A2 -0.481360070
  
  ```
  
  Does it answer your question ?
  
  > **Comments:**
  > **username_2**: No need for forloops, see my answer.  
  
  ---
  
  