1

Hi I'm a biologist and new to coding in R. I'm trying to write a small program for designer primers; you input a string of characters ATGC for DNA, and it reverses the order and swaps the DNA bases over( A becomes T, T becomes A, G becomes C, C become G). I can get it to give me back the opposite DNA to the first character in the string, but it doesn't loop through the whole string. Any help much appreciated!

primer <- function(input_string){
  last_position <- nchar(input_string)
  output <- ""
  for (position in 1:last_position) {
     one_character <- substr(input_string, position, position)
    if(one_character=="A"){
      new_character <- "T" }
    if (one_character=="T"){
      new_character <- "A"}
    if (one_character=="G"){
        new_character <- "C"}
    if (one_character=="C"){
      new_character <- "G"}
   output <- paste(new_character, output, sep="")
   return(output)
 }
}
  • 2
    Because you have `return` inside the loop. It returns (!) at the end of the first iteration. – Rui Barradas Oct 21 '17 at 17:26
  • `chartr(old = stringi::stri_reverse(c("ATGC")), new = c("TACG"), "ATGC")` – Henrik Oct 21 '17 at 19:59
  • Thanks, still getting the hang of curly brackets and once you said that I could straight away that the return was inside the loop. It's a steep learning curve for beginners! – Omgu8mynewt Oct 21 '17 at 22:00

0 Answers0