Hi community
I want to put spaces in each name according to the numbers that it has in its name.
Each name is encoded to have 5 spaces for 5 numbers and then a letter, for example G#####A.
I have a database that does not respect these spaces, all the numbers come together (column names
).
I try to count the number of letters and in names2
I put some spaces but it doesn't work very well for all rows. And other problem is beacause count the final letter.
The idea is to get the names as they appear in names_TRUE
, where the 5 spaces for the numbers are respected in each name.
df <- data.frame(names=c('G1', 'G5', 'G22','G52', 'G768A','G522','G3412',
'G3412B' , 'G51323C'))
df$NoCh <- nchar(df$names)
df <- df %>%
mutate(names2 = case_when(
NoCh == 2 ~ gsub("([G])", "\\1 ", df$names),
NoCh == 3 ~ gsub("([G])", "\\1 ", df$names),
NoCh == 4 ~ gsub("([G])", "\\1 ", df$names),
NoCh == 5 ~ gsub("([G])", "\\1 ", df$names),
NoCh == 6 ~ gsub("([G])", "\\1", df$names),
NoCh == 7 ~ gsub("([G])", "\\1", df$names)))
df$names_TRUE <- c('G 1', 'G 5', 'G 22','G 52', 'G 768A','G 522','G 3412',
'G 3412B' , 'G51323C')
df
# names NoCh names2 names_TRUE
# 1 G1 2 G 1 G 1
# 2 G5 2 G 5 G 5
# 3 G22 3 G 22 G 22
# 4 G52 3 G 52 G 52
# 5 G768A 5 G 768A G 768A
# 6 G522 4 G 522 G 522
# 7 G3412 5 G 3412 G 3412
# 8 G3412B 6 G3412B G 3412B
# 9 G51323C 7 G51323C G51323C
Tnks