在数字和单词 R 之间添加一个 space

Add a space between number and word R

我想在数字和单词R之间加一个space

例如,我希望 "1in10" "1 in 10"。并且,"9months""9 months"

非常感谢您的帮助。

mystring1 <- "1in10"

mystring2 <- "9months"

谢谢!

具有正则表达式环视的选项以匹配 digit/non-digit 或 non-digit/digit 之间的连接点并替换为 space

gsub("(?<=\d)(?=\D)|(?<=\D)(?=\d)", " ", str1, perl = TRUE)
#[1] "1 in 10"  "9 months"

数据

str1 <- c( "1in10", "9months")