PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 6, 2022

[FIXED] How to paste characters in groups of 3 in R

 November 06, 2022     r, regex-group, string     No comments   

Issue

Here is a DNA string that I want to split and then combine in groups of 3

dna=c("TACACGATGACAGTCTTGACGGGTTCTCCTACT")
dna.sg = unlist(strsplit(dna, ""))

Gives

 [1] "T" "A" "C" "A" "C" "G" "A" "T" "G" "A" "C" "A" "G" "T" "C" "T" "T" "G" "A" "C" "G" "G" "G" "T" "T" "C" "T" "C" "C" "T" "A" "C" "T"

But I'd like to have

"TAC" "ACG" [...]

Solution

You may split every 3 characters in strsplit.

unlist(strsplit(dna, "(?<=.{3})", perl = TRUE))
#[1] "TAC" "ACG" "ATG" "ACA" "GTC" "TTG" "ACG" "GGT" "TCT" "CCT" "ACT"


Answered By - Ronak Shah
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing