【发布时间】:2018-12-08 17:48:29
【问题描述】:
我有一个包含一些数字的字符串,并用一个随机数替换每个数字。
例如。 “111”应替换为 0-9 之间的 3 个随机数,它们像“364”一样连接起来。
我的想法是匹配一个数字,获取位数,计算尽可能多的随机数并将它们连接起来最终替换我匹配的数字:
test <- "this is 1 example 123. I like the no.37"
gsub("([0-9])", paste0(sample(0:9, nchar("\\1")), collapse = ""), test)
我的目标是创建一个字符串,其中每个数字都替换为随机数字。例如
"this is 3 an example 628. I like the no.09"
我尝试了一些方法,但找不到好的解决方案。
【问题讨论】: