Search code examples
rstringloopsreplacestrsplit

Loops in data using str_replace_all and strsplit in R


I have a dataframe with a column with the next caracteres:

 head(example)
  X.       S10   tactagcaatacgcttgcgttcggtggttaagtatgtataatgcgcgggcttgtcgt
1  +      AM \t\ttgctatcctgacagttgtcacgctgattggtgtcgttacaatctaacgcatcgccaa
2  +      AR \t\tgtactagagaactagtgcattagcttatttttttgttatcatgctaaccacccggcg
3  +     DE  \taattgtgatgtgtatcgaagtgtgttgcggagtagatgttagaatactaacaaactc

So I want replace each letter by specific chain. For example: when it appears "a" i want put 0,0,0,1. But I get it

str_replace_all(example, "[a]", c("0","0","0","1")) [1] "tgct0tcctg0c0gttgtc0cgctg0ttggtgtcgtt0c00tct00cgc0tcgcc00"


Also I need separate each value in one column. 

Solution

  • We can use a named vector with str_replace_all

    library(stringr)
    str_replace_all(example, setNames( c('0001', '1000'), c('a', 't'))) 
    

    It replaces the lettters 'a', 't' with the values '0001' and '1000' respectively