text: user4595: 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
  
  user6437: Using sub in base R, you can:
  1) define the replacement vector:
  vector = paste0("A",seq(1:21))
  
  2) and loop over each levels of your first columns to replace them individually:
  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:
  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
  
  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:
  > 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 ?
  
  user131: Change to factor then to numeric:
  mat$Tumor_Sample_Barcode <- as.numeric(as.factor(mat$Tumor_Sample_Barcode))
  
  Using @dc37 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
  
  
  user181: 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 @dc37]. 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.
  
original_text: 4595: <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>
   6437: <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>
   131: <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>
   181: <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>
  