Advertisements
I am working with R. Below you can see my code and my data:
df <- data.frame(
Column = c("0101100000", "2201205000"))
In the column my data is text which is composed of ten characters.
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
>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"