Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Put space between specific strings

I am working with R. Below you can see my code and my data:

df <- data.frame(
  Column = c("0101100000", "2201205000"))

enter image description here

In the column my data is text which is composed of ten characters.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Now I want to put space between first and second four characters. I tried with this line below but is not working.

df$Column1 <- gsub("\\s{2,}", " ", df$Column)

So can anybody help me how to solve this problem and to have data frame as data shown in the picture below

enter image description here

>Solution :

You can use gsub like so:

gsub('(.{4})', '\\1 ', df$Column)
#[1] "0101 1000 00" "2201 2050 00"

In some cases, this will add a trailing space; use trimws in that case:

trimws(gsub('(.{4})', '\\1 ', "02020202"))
#[1] "0202 0202"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading