如何在 r 中的数字和度量单位之间添加 space
How to add a space between digits and unit of measure in r
我有一个数据集,其中有时度量单位与数字之间没有 space 分隔,我想将其添加进去。我有一个可以使用的度量单位列表在数据集中,我想确保它们每次出现时都有一个 space。
我的数据是这样的:
mydata <- c("black box 125CM", "10KG white chair", "bottle of water 1000ML")
我想:
result <- c("black box 125 CM", "10 KG white chair", "bottle of water 1000 ML")
可能出现的计量单位:
measure <- c("ML", "MG", "F", "CM", "CPR", "FL", "CPS", "KG")
到目前为止我已经尝试过(但它不起作用):
for (i in 1:NROW(measure)) {
replacement <- paste0("\s", measure[i])
result <- gsub("(?<=[[:digit:]])"measure[i], replacement, mydata, perl = TRUE)
}
如果是一次替换,我可以这样做:
result <- gsub("(?<=[[:digit:]])MG", " MG", mydata, perl = TRUE)
我只是不知道应该如何在 gsub 函数中编写 measure[i]
,我找不到正确的语法。
有什么建议么?非常感谢您。
这是我想出的并且对我有用。
mydata <- c("black box 125CM", "10KG white chair", "bottle of water 1000ML")
measure <- c("ML", "MG", "F", "CM", "CPR", "FL", "CPS", "KG")
measure <- paste(measure, collapse = "|")
result <- sub(paste0("([", measure, "])"), " \1", mydata)
编辑:如果已经有 space,这也会添加 spaces,r2evans 解决方案会更可取。
Regex lookahead可以做到这一点。
gsub(paste0("(?<=[0-9])(", paste(measure, collapse = "|"), ")"), " \1",
mydata, perl = TRUE)
# [1] "black box 125 CM" "10 KG white chair" "bottle of water 1000 ML"
mydata <- c("black box 125CM", "10KG white chair", "bottle of water 1000ML")
stringr::str_replace_all(mydata, "[:digit:]([ML|MG|F|C[M|PR|PS]|FL|KG])", " \1")
给予
[1] "black box 12 CM" "1 KG white chair" "bottle of water 100 ML"
注意以C
开头的三种情况的特殊处理。
顺便说一句,如果我不得不对空间如此挑剔,我也会介意对正确使用 SI 单位的情况挑剔:“KG”不是千克,而是开尔文 ⋅ 6.674× 10−11m3⋅kg−1⋅s−2,尽我所能!
如果像示例中那样,度量总是出现在数字之后,那么这行得通:
sub("(\d+)", "\1 ", mydata)
[1] "black box 125 CM" "10 KG white chair" "bottle of water 1000 ML"
我有一个数据集,其中有时度量单位与数字之间没有 space 分隔,我想将其添加进去。我有一个可以使用的度量单位列表在数据集中,我想确保它们每次出现时都有一个 space。
我的数据是这样的:
mydata <- c("black box 125CM", "10KG white chair", "bottle of water 1000ML")
我想:
result <- c("black box 125 CM", "10 KG white chair", "bottle of water 1000 ML")
可能出现的计量单位:
measure <- c("ML", "MG", "F", "CM", "CPR", "FL", "CPS", "KG")
到目前为止我已经尝试过(但它不起作用):
for (i in 1:NROW(measure)) {
replacement <- paste0("\s", measure[i])
result <- gsub("(?<=[[:digit:]])"measure[i], replacement, mydata, perl = TRUE)
}
如果是一次替换,我可以这样做:
result <- gsub("(?<=[[:digit:]])MG", " MG", mydata, perl = TRUE)
我只是不知道应该如何在 gsub 函数中编写 measure[i]
,我找不到正确的语法。
有什么建议么?非常感谢您。
这是我想出的并且对我有用。
mydata <- c("black box 125CM", "10KG white chair", "bottle of water 1000ML")
measure <- c("ML", "MG", "F", "CM", "CPR", "FL", "CPS", "KG")
measure <- paste(measure, collapse = "|")
result <- sub(paste0("([", measure, "])"), " \1", mydata)
编辑:如果已经有 space,这也会添加 spaces,r2evans 解决方案会更可取。
Regex lookahead可以做到这一点。
gsub(paste0("(?<=[0-9])(", paste(measure, collapse = "|"), ")"), " \1",
mydata, perl = TRUE)
# [1] "black box 125 CM" "10 KG white chair" "bottle of water 1000 ML"
mydata <- c("black box 125CM", "10KG white chair", "bottle of water 1000ML")
stringr::str_replace_all(mydata, "[:digit:]([ML|MG|F|C[M|PR|PS]|FL|KG])", " \1")
给予
[1] "black box 12 CM" "1 KG white chair" "bottle of water 100 ML"
注意以C
开头的三种情况的特殊处理。
顺便说一句,如果我不得不对空间如此挑剔,我也会介意对正确使用 SI 单位的情况挑剔:“KG”不是千克,而是开尔文 ⋅ 6.674× 10−11m3⋅kg−1⋅s−2,尽我所能!
如果像示例中那样,度量总是出现在数字之后,那么这行得通:
sub("(\d+)", "\1 ", mydata)
[1] "black box 125 CM" "10 KG white chair" "bottle of water 1000 ML"