seq =" GAGTAGGAGGAG",如何将此序列分成以下子序列" GAG"," TAG"," GAG" ," GAG"即如何将序列分成三组
答案 0 :(得分:1)
我们可以创建一个名为fixed_split
的函数,它将字符串拆分成相等的部分。正则表达式是一个与n
元素匹配的lookbehind:
fixed_split <- function(text, n) {
strsplit(text, paste0("(?<=.{",n,"})"), perl=TRUE)
}
fixed_split("GAGTAGGAGGAG", 3)
[[1]]
[1] "GAG" "TAG" "GAG" "GAG"
修改强>
在评论中,您说sequence ="ATGATGATG"
不起作用:
strsplit(sequence,"(?<=.{3})", perl=TRUE)
[[1]]
[1] "ATG" "ATG" "ATG"