Home > Net >  How to replace characters in a vector in R?
How to replace characters in a vector in R?

Time:11-02

I have been tasked with writing an R function capable of taking a DNA string (s) as input and returning the complementary string on the reverse strand (e.g. "ACGT" returns "TGCA"). The result should look something like this:

     > s <- "CCCTTAG"
     > reverse.dna(s)
     [1] "CTAAGGG"

I currently have the following functions for converting a string to a vector and vice versa, however any attempts I have made to use the replace() or switch() commands to substitute the complementary bases into either the string or vector have been unsuccessful.

     string.to.vec <- function(s) {
        strsplit(s,"") [[1]]

     vec.to.string <- function(v) {
        paste(v,collapse="")

As I have very limited experience using R I was wondering if anyone would be able to help me by suggesting the simplest method to implement this functionality into my function. Thanks!

CodePudding user response:

We may use chartr in combination with stri_reverse

library(stringi)
chartr("ACGT", "TGCA", stri_reverse(s))
[1] "CTAAGGG"

CodePudding user response:

The Bioconductor Biostrings package has functions for DNA strings. We convert s from the question to a DNAString object, run reverseComplement and then convert back to character class. Omit the last conversion if you want to keep it as a DNAString.

library(Biostrings)

s |> DNAString() |> reverseComplement() |> as.character()
## [1] "CTAAGGG"
  • Related