1
$\begingroup$

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
   <chr>      <chr>      
 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 .

$\endgroup$
1
$\begingroup$

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.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ even though im accepting your answer it is reverting back some issue i guess $\endgroup$ – krushnach Chandra Feb 3 at 8:42
  • 1
    $\begingroup$ 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. $\endgroup$ – haci Feb 3 at 8:46
  • $\begingroup$ yes ,there are so many ways..its just that i lack the programming skills and that takes me a lot of time to run analysis $\endgroup$ – krushnach Chandra Feb 3 at 10:20
3
$\begingroup$

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.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ fabulous solution i spend less time in biology more time on R your solutions saves my time $\endgroup$ – krushnach Chandra Feb 3 at 8:33
  • 1
    $\begingroup$ @krushnachChandra any specific reason you changed the accepted answer? $\endgroup$ – zx8754 Feb 3 at 16:25
  • $\begingroup$ "any specific reason you changed the accepted answer?" actually my internet is having some issue im accepted it but it reverts back the same issue I pointed with the first answer sorry I accepted it $\endgroup$ – krushnach Chandra Feb 3 at 16:55
  • $\begingroup$ same issue with the first answer Im clicking it turns green then back to grey $\endgroup$ – krushnach Chandra Feb 3 at 16:57
  • $\begingroup$ here is the screenshot ibb.co/vZLMn3w you can see ..not sure if my browser issue or internet $\endgroup$ – krushnach Chandra Feb 3 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.