<p>EDIT: Wheres the OP accepted this answer, @zx8754's answer below, as well as the ones in the link they gave, are much more elegant and straightforward.</p>

<hr>

<p>Here is a work-around with <code>table()</code>, <code>lapply()</code> and <code>seq_len()</code> (I have basically used your <code>lapply()</code> call on the output of a <code>table()</code> call):    </p>

<pre><code>my_counts &lt;- table(df1$source_name)

&gt; my_counts

Blast   CMP   GMP   HSC   LSC  Mono  pHSC 
   23     8     7     7     8     6    19 

df1 &lt;- df1[order(df1$source_name),]

&gt; head(df1)
          Run source_name
37 SRR2920546       Blast
38 SRR2920547       Blast
39 SRR2920548       Blast
40 SRR2920549       Blast
41 SRR2920550       Blast
42 SRR2920551       Blast

&gt; names(unlist(lapply(my_counts, seq_len)))
 [1] "Blast1"  "Blast2"  "Blast3"  "Blast4"  "Blast5"  "Blast6"  "Blast7"  "Blast8"  "Blast9"  "Blast10" "Blast11" "Blast12" "Blast13"
[14] "Blast14" "Blast15" "Blast16" "Blast17" "Blast18" "Blast19" "Blast20" "Blast21" "Blast22" "Blast23" "CMP1"    "CMP2"    "CMP3"   
[27] "CMP4"    "CMP5"    "CMP6"    "CMP7"    "CMP8"    "GMP1"    "GMP2"    "GMP3"    "GMP4"    "GMP5"    "GMP6"    "GMP7"    "HSC1"   
[40] "HSC2"    "HSC3"    "HSC4"    "HSC5"    "HSC6"    "HSC7"    "LSC1"    "LSC2"    "LSC3"    "LSC4"    "LSC5"    "LSC6"    "LSC7"   
[53] "LSC8"    "Mono1"   "Mono2"   "Mono3"   "Mono4"   "Mono5"   "Mono6"   "pHSC1"   "pHSC2"   "pHSC3"   "pHSC4"   "pHSC5"   "pHSC6"  
[66] "pHSC7"   "pHSC8"   "pHSC9"   "pHSC10"  "pHSC11"  "pHSC12"  "pHSC13"  "pHSC14"  "pHSC15"  "pHSC16"  "pHSC17"  "pHSC18"  "pHSC19"

df1$new_col &lt;- names(unlist(lapply(my_counts, seq_len)))

&gt; head(df1)
          Run source_name new_col
37 SRR2920546       Blast  Blast1
38 SRR2920547       Blast  Blast2
39 SRR2920548       Blast  Blast3
40 SRR2920549       Blast  Blast4
41 SRR2920550       Blast  Blast5
42 SRR2920551       Blast  Blast6
</code></pre>

<p>You can store the initial row order somewhere to have resulting data frame in the same order the initial one.</p>

CC BY-SA 4.0
2020-02-03T08:46:44.303Z
While my answer solves your problem, the `data.table` solution suggested by @zx8754 is much more elegant and should be the accepted answer in my opinion. I have up-voted that one anyway.
haci
CC BY-SA 4.0
2020-02-03T07:07:09.150Z
2020-02-08T12:34:45.967Z
2020-02-08T12:34:45.967Z
haci
<p>We need to group by <em>source_name</em> and add row id, here is using <em>data.table</em> package:</p>

<pre><code>library(data.table)

# convert to data.table
setDT(df1)

# group by source_name, and add rowid
df1[, myNewColumn := paste0(source_name, rowid(source_name))]

df1
#            Run source_name myNewColumn
#  1: SRR2920466         HSC        HSC1
#  2: SRR2920477         HSC        HSC2
#  3: SRR2920478         HSC        HSC3
#  4: SRR2920506         HSC        HSC4
#  5: SRR2920507         HSC        HSC5
#  6: SRR2920532         HSC        HSC6
#  7: SRR2920531         HSC        HSC7
#  8: SRR2920469         CMP        CMP1
#  9: SRR2920470         CMP        CMP2
# 10: SRR2920481         CMP        CMP3
# ...
</code></pre>

<p>See <a href="https://stackoverflow.com/q/12925063/680068">here</a> for more solutions.</p>

CC BY-SA 4.0
2020-02-03T07:41:22.420Z
2020-02-03T07:41:22.420Z
zx8754
<p>This is my data 
 dput(METADATA)</p>

<pre><code>structure(list(Run = c("SRR2920466", "SRR2920477", "SRR2920478", 
"SRR2920506", "SRR2920507", "SRR2920532", "SRR2920531", "SRR2920469", 
"SRR2920470", "SRR2920481", "SRR2920482", "SRR2920500", "SRR2920501", 
"SRR2920536", "SRR2920537", "SRR2920471", "SRR2920472", "SRR2920483", 
"SRR2920484", "SRR2920505", "SRR2920538", "SRR2920539", "SRR2920475", 
"SRR2920476", "SRR2920487", "SRR2920488", "SRR2920542", "SRR2920543", 
"SRR2920555", "SRR2920559", "SRR2920570", "SRR2920573", "SRR2920578", 
"SRR2920583", "SRR2920586", "SRR2920594", "SRR2920546", "SRR2920547", 
"SRR2920548", "SRR2920549", "SRR2920550", "SRR2920551", "SRR2920553", 
"SRR2920558", "SRR2920563", "SRR2920565", "SRR2920567", "SRR2920569", 
"SRR2920572", "SRR2920575", "SRR2920577", "SRR2920580", "SRR2920582", 
"SRR2920585", "SRR2920589", "SRR2920590", "SRR2920591", "SRR2920593", 
"SRR2920554", "SRR2920552", "SRR2920556", "SRR2920557", "SRR2920560", 
"SRR2920561", "SRR2920562", "SRR2920564", "SRR2920566", "SRR2920568", 
"SRR2920571", "SRR2920574", "SRR2920576", "SRR2920581", "SRR2920584", 
"SRR2920587", "SRR2920588", "SRR2920592", "SRR2920595", "SRR2920579"
), source_name = c("HSC", "HSC", "HSC", "HSC", "HSC", "HSC", 
"HSC", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", 
"GMP", "GMP", "GMP", "GMP", "GMP", "GMP", "GMP", "Mono", "Mono", 
"Mono", "Mono", "Mono", "Mono", "LSC", "LSC", "LSC", "LSC", "LSC", 
"LSC", "LSC", "LSC", "Blast", "Blast", "Blast", "Blast", "Blast", 
"Blast", "Blast", "Blast", "Blast", "Blast", "Blast", "Blast", 
"Blast", "Blast", "Blast", "Blast", "Blast", "Blast", "Blast", 
"Blast", "Blast", "Blast", "Blast", "pHSC", "pHSC", "pHSC", "pHSC", 
"pHSC", "pHSC", "pHSC", "pHSC", "pHSC", "pHSC", "pHSC", "pHSC", 
"pHSC", "pHSC", "pHSC", "pHSC", "pHSC", "pHSC", "pHSC")), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -78L), spec = structure(list(
    cols = list(Run = structure(list(), class = c("collector_character", 
    "collector")), `Assay Type` = structure(list(), class = c("collector_character", 
    "collector")), AvgSpotLen = structure(list(), class = c("collector_double", 
    "collector")), BioProject = structure(list(), class = c("collector_character", 
    "collector")), BioSample = structure(list(), class = c("collector_character", 
    "collector")), Cell_type = structure(list(), class = c("collector_character", 
    "collector")), `Center Name` = structure(list(), class = c("collector_character", 
    "collector")), Consent = structure(list(), class = c("collector_character", 
    "collector")), `DATASTORE filetype` = structure(list(), class = c("collector_character", 
    "collector")), `DATASTORE provider` = structure(list(), class = c("collector_character", 
    "collector")), `DATASTORE region` = structure(list(), class = c("collector_character", 
    "collector")), donorid = structure(list(), class = c("collector_character", 
    "collector")), Experiment = structure(list(), class = c("collector_character", 
    "collector")), GEO_Accession = structure(list(), class = c("collector_character", 
    "collector")), Instrument = structure(list(), class = c("collector_character", 
    "collector")), LibraryLayout = structure(list(), class = c("collector_character", 
    "collector")), LibrarySelection = structure(list(), class = c("collector_character", 
    "collector")), LibrarySource = structure(list(), class = c("collector_character", 
    "collector")), MBases = structure(list(), class = c("collector_double", 
    "collector")), MBytes = structure(list(), class = c("collector_double", 
    "collector")), Organism = structure(list(), class = c("collector_character", 
    "collector")), Platform = structure(list(), class = c("collector_character", 
    "collector")), ReleaseDate = structure(list(format = ""), class = c("collector_datetime", 
    "collector")), sample_acc = structure(list(), class = c("collector_character", 
    "collector")), `Sample Name` = structure(list(), class = c("collector_character", 
    "collector")), sample_type = structure(list(), class = c("collector_character", 
    "collector")), source_name = structure(list(), class = c("collector_character", 
    "collector")), `SRA Study` = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))
</code></pre>

<p>I want to have the below output to be in my final dataframe which i want to use for renaming the original file.</p>

<pre><code> Run        source_name
   &lt;chr&gt;      &lt;chr&gt;      
 1 SRR2920466 HSC1        
 2 SRR2920477 HSC2       
 3 SRR2920478 HSC3        
 4 SRR2920506 HSC4        
 5 SRR2920507 HSC5        
 6 SRR2920532 HSC6        
 7 SRR2920531 HSC7        
 8 SRR2920469 CMP1        
 9 SRR2920470 CMP2        
10 SRR2920481 CMP3    
</code></pre>

<p>Found a solution but I have to apply it to each string in the last column</p>

<pre><code>i1 &lt;- !sapply(df1, is.numeric)
df1[i1] &lt;- lapply(df1[i1], function(x) factor(replace(as.character(x),
                                                      x == "HSC", paste0(x[x=="HSC"], seq_len(sum(x == "HSC"))))))
</code></pre>

<p>Right now I am able to match repeated string in the column and add increment.How to apply it for all the other string .</p>

<r>
How to add an increment to a repeated strings
# How to add an increment to a repeated strings

(Asked by: username_0 on 2020-02-03)

This is my data 
 dput(METADATA)
structure(list(Run = c("SRR2920466", "SRR2920477", "SRR2920478", 
"SRR2920506", "SRR2920507", "SRR2920532", "SRR2920531", "SRR2920469", 
"SRR2920470", "SRR2920481", "SRR2920482", "SRR2920500", "SRR2920501", 
"SRR2920536", "SRR2920537", "SRR2920471", "SRR2920472", "SRR2920483", 
"SRR2920484", "SRR2920505", "SRR2920538", "SRR2920539", "SRR2920475", 
"SRR2920476", "SRR2920487", "SRR2920488", "SRR2920542", "SRR2920543", 
"SRR2920555", "SRR2920559", "SRR2920570", "SRR2920573", "SRR2920578", 
"SRR2920583", "SRR2920586", "SRR2920594", "SRR2920546", "SRR2920547", 
"SRR2920548", "SRR2920549", "SRR2920550", "SRR2920551", "SRR2920553", 
"SRR2920558", "SRR2920563", "SRR2920565", "SRR2920567", "SRR2920569", 
"SRR2920572", "SRR2920575", "SRR2920577", "SRR2920580", "SRR2920582", 
"SRR2920585", "SRR2920589", "SRR2920590", "SRR2920591", "SRR2920593", 
"SRR2920554", "SRR2920552", "SRR2920556", "SRR2920557", "SRR2920560", 
"SRR2920561", "SRR2920562", "SRR2920564", "SRR2920566", "SRR2920568", 
"SRR2920571", "SRR2920574", "SRR2920576", "SRR2920581", "SRR2920584", 
"SRR2920587", "SRR2920588", "SRR2920592", "SRR2920595", "SRR2920579"
), source_name = c("HSC", "HSC", "HSC", "HSC", "HSC", "HSC", 
"HSC", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", 
"GMP", "GMP", "GMP", "GMP", "GMP", "GMP", "GMP", "Mono", "Mono", 
"Mono", "Mono", "Mono", "Mono", "LSC", "LSC", "LSC", "LSC", "LSC", 
"LSC", "LSC", "LSC", "Blast", "Blast", "Blast", "Blast", "Blast", 
"Blast", "Blast", "Blast", "Blast", "Blast", "Blast", "Blast", 
"Blast", "Blast", "Blast", "Blast", "Blast", "Blast", "Blast", 
"Blast", "Blast", "Blast", "Blast", "pHSC", "pHSC", "pHSC", "pHSC", 
"pHSC", "pHSC", "pHSC", "pHSC", "pHSC", "pHSC", "pHSC", "pHSC", 
"pHSC", "pHSC", "pHSC", "pHSC", "pHSC", "pHSC", "pHSC")), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -78L), spec = structure(list(
    cols = list(Run = structure(list(), class = c("collector_character", 
    "collector")), `Assay Type` = structure(list(), class = c("collector_character", 
    "collector")), AvgSpotLen = structure(list(), class = c("collector_double", 
    "collector")), BioProject = structure(list(), class = c("collector_character", 
    "collector")), BioSample = structure(list(), class = c("collector_character", 
    "collector")), Cell_type = structure(list(), class = c("collector_character", 
    "collector")), `Center Name` = structure(list(), class = c("collector_character", 
    "collector")), Consent = structure(list(), class = c("collector_character", 
    "collector")), `DATASTORE filetype` = structure(list(), class = c("collector_character", 
    "collector")), `DATASTORE provider` = structure(list(), class = c("collector_character", 
    "collector")), `DATASTORE region` = structure(list(), class = c("collector_character", 
    "collector")), donorid = structure(list(), class = c("collector_character", 
    "collector")), Experiment = structure(list(), class = c("collector_character", 
    "collector")), GEO_Accession = structure(list(), class = c("collector_character", 
    "collector")), Instrument = structure(list(), class = c("collector_character", 
    "collector")), LibraryLayout = structure(list(), class = c("collector_character", 
    "collector")), LibrarySelection = structure(list(), class = c("collector_character", 
    "collector")), LibrarySource = structure(list(), class = c("collector_character", 
    "collector")), MBases = structure(list(), class = c("collector_double", 
    "collector")), MBytes = structure(list(), class = c("collector_double", 
    "collector")), Organism = structure(list(), class = c("collector_character", 
    "collector")), Platform = structure(list(), class = c("collector_character", 
    "collector")), ReleaseDate = structure(list(format = ""), class = c("collector_datetime", 
    "collector")), sample_acc = structure(list(), class = c("collector_character", 
    "collector")), `Sample Name` = structure(list(), class = c("collector_character", 
    "collector")), sample_type = structure(list(), class = c("collector_character", 
    "collector")), source_name = structure(list(), class = c("collector_character", 
    "collector")), `SRA Study` = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))

I want to have the below output to be in my final dataframe which i want to use for renaming the original file.
 Run        source_name
               
 1 SRR2920466 HSC1        
 2 SRR2920477 HSC2       
 3 SRR2920478 HSC3        
 4 SRR2920506 HSC4        
 5 SRR2920507 HSC5        
 6 SRR2920532 HSC6        
 7 SRR2920531 HSC7        
 8 SRR2920469 CMP1        
 9 SRR2920470 CMP2        
10 SRR2920481 CMP3    

Found a solution but I have to apply it to each string in the last column
i1 <- !sapply(df1, is.numeric)
df1[i1] <- lapply(df1[i1], function(x) factor(replace(as.character(x),
                                                      x == "HSC", paste0(x[x=="HSC"], seq_len(sum(x == "HSC"))))))

Right now I am able to match repeated string in the column and add increment.How to apply it for all the other string .

**Tags:** r

---

## 2 answers

### Answer by username_2 on 2020-02-03. Score: 3 | Accepted answer

We need to group by source_name and add row id, here is using data.table package:
library(data.table)

# convert to data.table
setDT(df1)

# group by source_name, and add rowid
df1[, myNewColumn := paste0(source_name, rowid(source_name))]

df1
#            Run source_name myNewColumn
#  1: SRR2920466         HSC        HSC1
#  2: SRR2920477         HSC        HSC2
#  3: SRR2920478         HSC        HSC3
#  4: SRR2920506         HSC        HSC4
#  5: SRR2920507         HSC        HSC5
#  6: SRR2920532         HSC        HSC6
#  7: SRR2920531         HSC        HSC7
#  8: SRR2920469         CMP        CMP1
#  9: SRR2920470         CMP        CMP2
# 10: SRR2920481         CMP        CMP3
# ...

See here for more solutions.


---

### Answer by username_1 on 2020-02-03. Score: 1

EDIT: Wheres the OP accepted this answer, @zx8754's answer below, as well as the ones in the link they gave, are much more elegant and straightforward.

Here is a work-around with table(), lapply() and seq_len() (I have basically used your lapply() call on the output of a table() call):    
my_counts <- table(df1$source_name)

> my_counts

Blast   CMP   GMP   HSC   LSC  Mono  pHSC 
   23     8     7     7     8     6    19 

df1 <- df1[order(df1$source_name),]

> head(df1)
          Run source_name
37 SRR2920546       Blast
38 SRR2920547       Blast
39 SRR2920548       Blast
40 SRR2920549       Blast
41 SRR2920550       Blast
42 SRR2920551       Blast

> names(unlist(lapply(my_counts, seq_len)))
 [1] "Blast1"  "Blast2"  "Blast3"  "Blast4"  "Blast5"  "Blast6"  "Blast7"  "Blast8"  "Blast9"  "Blast10" "Blast11" "Blast12" "Blast13"
[14] "Blast14" "Blast15" "Blast16" "Blast17" "Blast18" "Blast19" "Blast20" "Blast21" "Blast22" "Blast23" "CMP1"    "CMP2"    "CMP3"   
[27] "CMP4"    "CMP5"    "CMP6"    "CMP7"    "CMP8"    "GMP1"    "GMP2"    "GMP3"    "GMP4"    "GMP5"    "GMP6"    "GMP7"    "HSC1"   
[40] "HSC2"    "HSC3"    "HSC4"    "HSC5"    "HSC6"    "HSC7"    "LSC1"    "LSC2"    "LSC3"    "LSC4"    "LSC5"    "LSC6"    "LSC7"   
[53] "LSC8"    "Mono1"   "Mono2"   "Mono3"   "Mono4"   "Mono5"   "Mono6"   "pHSC1"   "pHSC2"   "pHSC3"   "pHSC4"   "pHSC5"   "pHSC6"  
[66] "pHSC7"   "pHSC8"   "pHSC9"   "pHSC10"  "pHSC11"  "pHSC12"  "pHSC13"  "pHSC14"  "pHSC15"  "pHSC16"  "pHSC17"  "pHSC18"  "pHSC19"

df1$new_col <- names(unlist(lapply(my_counts, seq_len)))

> head(df1)
          Run source_name new_col
37 SRR2920546       Blast  Blast1
38 SRR2920547       Blast  Blast2
39 SRR2920548       Blast  Blast3
40 SRR2920549       Blast  Blast4
41 SRR2920550       Blast  Blast5
42 SRR2920551       Blast  Blast6

You can store the initial row order somewhere to have resulting data frame in the same order the initial one.

> **Comments:**
> **username_1**: While my answer solves your problem, the `data.table` solution suggested by @zx8754 is much more elegant and should be the accepted answer in my opinion. I have up-voted that one anyway.  

---

