我需要将字符串从字符串右侧分成4个字符的组,并用逗号分隔。
示例:"5707559"
需要显示为0570,7559
,如果字符串是奇数(3个字符),则添加前导零。
原始字符串(需要分隔的字符串)具有来自dataframe列的可变长度:
Strings Fixed_Strings
5707559 0570, 7559
7502 7502
302 0302
你可以试试这个:
split_by_n <- function(string, n = 4) {
missing_zeros_number <- (n - nchar(string)) %% n
extended_string_with_zeros <- paste0(
c(numeric(missing_zeros_number), string),
collapse = "")
total_length <- nchar(extended_string_with_zeros)
substring(
extended_string_with_zeros,
seq(1, total_length, n),
seq(n, total_length, n)
)
}
split_by_n("foo")
split_by_n("foobar")
# [1] "0foo"
# [1] "00fo" "obar"
最后在矢量上使用:
lapply(c("foo", "foobar"), split_by_n)
#[[1]]
#[1] "0foo"
#
#[[2]]
#[1] "00fo" "obar"
x = c("5707559", "7502", "302")
ngroup = 4
library(stringr)
y = str_pad(x, width = ngroup*ceiling(nchar(x)/ngroup), pad = "0")
sapply(y, function(s) {
n = nchar(s)/ngroup
paste(sapply(seq(n), function(i)
substring(s, ngroup*i-(ngroup - 1), ngroup*i)), collapse = ",")
})
# 05707559 7502 0302
#"0570,7559" "7502" "0302"