我需要将一个字符串从字符串的右侧撒进4个字符的组中,并以逗号分隔。
示例:
"5707559"
需要显示为0570,7559
,如果字符串是奇数(3个字符),则添加前导零。
原始字符串(需要分隔的字符串)在数据框列中的长度是可变的:
Strings Fixed_Strings
5707559 0570, 7559
7502 7502
302 0302
答案 0 :(得分:0)
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"
答案 1 :(得分:0)
您可以尝试以下方法:
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"
最后在vector上使用:
lapply(c("foo", "foobar"), split_by_n)
#[[1]]
#[1] "0foo"
#
#[[2]]
#[1] "00fo" "obar"