使用 gsub 的互补序列

Die*_*hel 2 r gsub

我正在尝试将 DNA 链的互补序列存储在向量中。它应该将“A”更改为“T”,将“C”更改为“G”,反之亦然,问题是,我需要这发生在第一个向量上并正确打印互补序列。这是我尝试过但被卡住的:

pilot_sequence <- c("C","G","A","T","C","C","T","A","T")

complement_sequence_display <- function(pilot_sequence){
  complement_chain_Incom <- gsub("A", "T", pilot_sequence)
  complement_chain <- paste(complement_chain_Incom, collapse = "")
  cat("Complement sequence: ", complement_chain, "\n")
} 
complement_chain_Incom <- gsub("A","T", pilot_sequence)
complement_chain <- paste(complement_chain_Incom, collapse= "")
complement_sequence_display(pilot_sequence)
Run Code Online (Sandbox Code Playgroud)

我得到的答案是:CGTTCCTTT,只有第二个和倒数第二个 T 是正确的,我该如何解决其余字母?

Pilot_sequence向量是字符类型,并且函数没有显示执行错误。

Tar*_*Jae 5

这是函数的理想用例chartr

chartr("ATGC","TACG",pilot_sequence)
Run Code Online (Sandbox Code Playgroud)

输出:

[1] "G" "C" "T" "A" "G" "G" "A" "T" "A"
Run Code Online (Sandbox Code Playgroud)